home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 24 / CU Amiga Magazine's Super CD-ROM 24 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-07].iso / CUCD / Utilities / vim-5.1 / src / ex_cmds.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-06  |  70.3 KB  |  2,910 lines

  1. /* vi:set ts=8 sts=4 sw=4:
  2.  *
  3.  * VIM - Vi IMproved    by Bram Moolenaar
  4.  *
  5.  * Do ":help uganda"  in Vim to read copying and usage conditions.
  6.  * Do ":help credits" in Vim to see a list of people who contributed.
  7.  */
  8.  
  9. /*
  10.  * ex_cmds.c: functions for command line commands
  11.  */
  12.  
  13. #include "vim.h"
  14.  
  15. #ifdef EX_EXTRA
  16. static int linelen __ARGS((int *has_tab));
  17. #endif
  18. static void do_filter __ARGS((linenr_t line1, linenr_t line2,
  19.                     char_u *buff, int do_in, int do_out));
  20. #ifdef VIMINFO
  21. static char_u *viminfo_filename __ARGS((char_u    *));
  22. static void do_viminfo __ARGS((FILE *fp_in, FILE *fp_out, int want_info,
  23.                          int want_marks, int force_read));
  24. static int read_viminfo_up_to_marks __ARGS((char_u *line, FILE *fp,
  25.                            int forceit, int writing));
  26. #endif
  27.  
  28. static int do_sub_msg __ARGS((void));
  29. static int
  30. #ifdef __BORLANDC__
  31. _RTLENTRYF
  32. #endif
  33. help_compare __ARGS((const void *s1, const void *s2));
  34.  
  35.     void
  36. do_ascii()
  37. {
  38.     int        c;
  39.     char    buf1[20];
  40.     char    buf2[20];
  41.     char_u    buf3[3];
  42.  
  43.     c = gchar_cursor();
  44.     if (c == NUL)
  45.     {
  46.     MSG("empty line");
  47.     return;
  48.     }
  49.     if (c == NL)        /* NUL is stored as NL */
  50.     c = NUL;
  51.     if (vim_isprintc(c) && (c < ' ' || c > '~'))
  52.     {
  53.     transchar_nonprint(buf3, c);
  54.     sprintf(buf1, "  <%s>", (char *)buf3);
  55.     }
  56.     else
  57.     buf1[0] = NUL;
  58.     if (c >= 0x80)
  59.     sprintf(buf2, "  <M-%s>", transchar(c & 0x7f));
  60.     else
  61.     buf2[0] = NUL;
  62.     sprintf((char *)IObuff, "<%s>%s%s  %d,  Hex %02x,  Octal %03o",
  63.                        transchar(c), buf1, buf2, c, c, c);
  64.     msg(IObuff);
  65. }
  66.  
  67. #ifdef EX_EXTRA
  68. /*
  69.  * Handle ":left", ":center" and ":right" commands: align text.
  70.  */
  71.     void
  72. do_align(eap)
  73.     EXARG    *eap;
  74. {
  75.     FPOS    save_curpos;
  76.     int        len;
  77.     int        indent = 0;
  78.     int        new_indent;
  79.     int        has_tab;
  80.     int        width;
  81.  
  82. #ifdef RIGHTLEFT
  83.     if (curwin->w_p_rl)
  84.     {
  85.     /* switch left and right aligning */
  86.     if (eap->cmdidx == CMD_right)
  87.         eap->cmdidx = CMD_left;
  88.     else if (eap->cmdidx == CMD_left)
  89.         eap->cmdidx = CMD_right;
  90.     }
  91. #endif
  92.  
  93.     width = atoi((char *)eap->arg);
  94.     save_curpos = curwin->w_cursor;
  95.     if (eap->cmdidx == CMD_left)    /* width is used for new indent */
  96.     {
  97.     if (width >= 0)
  98.         indent = width;
  99.     }
  100.     else
  101.     {
  102.     /*
  103.      * if 'textwidth' set, use it
  104.      * else if 'wrapmargin' set, use it
  105.      * if invalid value, use 80
  106.      */
  107.     if (width <= 0)
  108.         width = curbuf->b_p_tw;
  109.     if (width == 0 && curbuf->b_p_wm > 0)
  110.         width = Columns - curbuf->b_p_wm;
  111.     if (width <= 0)
  112.         width = 80;
  113.     }
  114.  
  115.     if (u_save((linenr_t)(eap->line1 - 1), (linenr_t)(eap->line2 + 1)) == FAIL)
  116.     return;
  117.     for (curwin->w_cursor.lnum = eap->line1;
  118.          curwin->w_cursor.lnum <= eap->line2; ++curwin->w_cursor.lnum)
  119.     {
  120.     if (eap->cmdidx == CMD_left)        /* left align */
  121.         new_indent = indent;
  122.     else
  123.     {
  124.         len = linelen(eap->cmdidx == CMD_right ? &has_tab
  125.                            : NULL) - get_indent();
  126.  
  127.         if (len <= 0)            /* skip blank lines */
  128.         continue;
  129.  
  130.         if (eap->cmdidx == CMD_center)
  131.         new_indent = (width - len) / 2;
  132.         else
  133.         {
  134.         new_indent = width - len;    /* right align */
  135.  
  136.         /*
  137.          * Make sure that embedded TABs don't make the text go too far
  138.          * to the right.
  139.          */
  140.         if (has_tab)
  141.             while (new_indent > 0)
  142.             {
  143.             set_indent(new_indent, TRUE);    /* set indent */
  144.             if (linelen(NULL) <= width)
  145.             {
  146.                 /*
  147.                  * Now try to move the line as much as possible to
  148.                  * the right.  Stop when it moves too far.
  149.                  */
  150.                 do
  151.                 set_indent(++new_indent, TRUE);    /* set indent */
  152.                 while (linelen(NULL) <= width);
  153.                 --new_indent;
  154.                 break;
  155.             }
  156.             --new_indent;
  157.             }
  158.         }
  159.     }
  160.     if (new_indent < 0)
  161.         new_indent = 0;
  162.     set_indent(new_indent, TRUE);        /* set indent */
  163.     }
  164.     curwin->w_cursor = save_curpos;
  165.     beginline(BL_WHITE | BL_FIX);
  166.  
  167.     /*
  168.      * If the cursor is after the first changed line, its position needs to be
  169.      * updated.
  170.      */
  171.     if (curwin->w_cursor.lnum > eap->line1)
  172.     {
  173.     changed_line_abv_curs();
  174.     invalidate_botline();
  175.     }
  176.     else if (curwin->w_cursor.lnum == eap->line1)
  177.     changed_cline_bef_curs();
  178.  
  179.     /*
  180.      * If the start of the aligned lines is before botline, it may have become
  181.      * approximated (lines got longer or shorter).
  182.      */
  183.     if (botline_approximated() && eap->line1 < curwin->w_botline)
  184.     approximate_botline();
  185.     update_screen(NOT_VALID);
  186. }
  187.  
  188. /*
  189.  * Get the length of the current line, excluding trailing white space.
  190.  */
  191.     static int
  192. linelen(has_tab)
  193.     int        *has_tab;
  194. {
  195.     char_u  *line;
  196.     char_u  *first;
  197.     char_u  *last;
  198.     int        save;
  199.     int        len;
  200.  
  201.     /* find the first non-blank character */
  202.     line = ml_get_curline();
  203.     first = skipwhite(line);
  204.  
  205.     /* find the character after the last non-blank character */
  206.     for (last = first + STRLEN(first);
  207.                 last > first && vim_iswhite(last[-1]); --last)
  208.     ;
  209.     save = *last;
  210.     *last = NUL;
  211.     len = linetabsize(line);        /* get line length */
  212.     if (has_tab != NULL)        /* check for embedded TAB */
  213.     *has_tab = (vim_strrchr(first, TAB) != NULL);
  214.     *last = save;
  215.  
  216.     return len;
  217. }
  218.  
  219. /*
  220.  * Handle ":retab" command.
  221.  */
  222.     void
  223. do_retab(eap)
  224.     EXARG    *eap;
  225. {
  226.     linenr_t    lnum;
  227.     int        got_tab = FALSE;
  228.     long    num_spaces = 0;
  229.     long    num_tabs;
  230.     long    len;
  231.     long    col;
  232.     long    vcol;
  233.     long    start_col = 0;        /* For start of white-space string */
  234.     long    start_vcol = 0;        /* For start of white-space string */
  235.     int        temp;
  236.     long    old_len;
  237.     char_u    *ptr;
  238.     char_u    *new_line = (char_u *)1;    /* init to non-NULL */
  239.     int        did_something = FALSE;
  240.     int        did_undo;        /* called u_save for current line */
  241.     int        new_ts;
  242.     int        save_list;
  243.  
  244.     save_list = curwin->w_p_list;
  245.     curwin->w_p_list = 0;        /* don't want list mode here */
  246.  
  247.     new_ts = getdigits(&(eap->arg));
  248.     if (new_ts == 0)
  249.     new_ts = curbuf->b_p_ts;
  250.     for (lnum = eap->line1; !got_int && lnum <= eap->line2; ++lnum)
  251.     {
  252.     ptr = ml_get(lnum);
  253.     col = 0;
  254.     vcol = 0;
  255.     did_undo = FALSE;
  256.     for (;;)
  257.     {
  258.         if (vim_iswhite(ptr[col]))
  259.         {
  260.         if (!got_tab && num_spaces == 0)
  261.         {
  262.             /* First consecutive white-space */
  263.             start_vcol = vcol;
  264.             start_col = col;
  265.         }
  266.         if (ptr[col] == ' ')
  267.             num_spaces++;
  268.         else
  269.             got_tab = TRUE;
  270.         }
  271.         else
  272.         {
  273.         if (got_tab || (eap->forceit && num_spaces > 1))
  274.         {
  275.             /* Retabulate this string of white-space */
  276.  
  277.             /* len is virtual length of white string */
  278.             len = num_spaces = vcol - start_vcol;
  279.             num_tabs = 0;
  280.             if (!curbuf->b_p_et)
  281.             {
  282.             temp = new_ts - (start_vcol % new_ts);
  283.             if (num_spaces >= temp)
  284.             {
  285.                 num_spaces -= temp;
  286.                 num_tabs++;
  287.             }
  288.             num_tabs += num_spaces / new_ts;
  289.             num_spaces -= (num_spaces / new_ts) * new_ts;
  290.             }
  291.             if (curbuf->b_p_et || got_tab ||
  292.                     (num_spaces + num_tabs < len))
  293.             {
  294.             if (did_undo == FALSE)
  295.             {
  296.                 did_undo = TRUE;
  297.                 if (u_save((linenr_t)(lnum - 1),
  298.                         (linenr_t)(lnum + 1)) == FAIL)
  299.                 {
  300.                 new_line = NULL;    /* flag out-of-memory */
  301.                 break;
  302.                 }
  303.             }
  304.  
  305.             /* len is actual number of white characters used */
  306.             len = num_spaces + num_tabs;
  307.             old_len = STRLEN(ptr);
  308.             new_line = lalloc(old_len - col + start_col + len + 1,
  309.                                     TRUE);
  310.             if (new_line == NULL)
  311.                 break;
  312.             if (start_col > 0)
  313.                 vim_memmove(new_line, ptr, (size_t)start_col);
  314.             vim_memmove(new_line + start_col + len,
  315.                       ptr + col, (size_t)(old_len - col + 1));
  316.             ptr = new_line + start_col;
  317.             for (col = 0; col < len; col++)
  318.                 ptr[col] = (col < num_tabs) ? '\t' : ' ';
  319.             ml_replace(lnum, new_line, FALSE);
  320.             did_something = TRUE;
  321.             ptr = new_line;
  322.             col = start_col + len;
  323.             }
  324.         }
  325.         got_tab = FALSE;
  326.         num_spaces = 0;
  327.         }
  328.         if (ptr[col] == NUL)
  329.         break;
  330.         vcol += chartabsize(ptr[col++], (colnr_t)vcol);
  331.     }
  332.     if (new_line == NULL)            /* out of memory */
  333.         break;
  334.     line_breakcheck();
  335.     }
  336.     if (got_int)
  337.     emsg(e_interr);
  338.     if (did_something)
  339.     CHANGED;
  340.  
  341.     curwin->w_p_list = save_list;    /* restore 'list' */
  342.  
  343.     if (curbuf->b_p_ts != new_ts || did_something)
  344.     {
  345.     /*
  346.      * Cursor may need updating when change is before or at the cursor
  347.      * line.  w_botline may be wrong a bit now.
  348.      */
  349.     if (curbuf->b_p_ts != new_ts || eap->line1 < curwin->w_cursor.lnum)
  350.         changed_line_abv_curs();        /* recompute cursor pos compl. */
  351.     else if (eap->line1 == curwin->w_cursor.lnum)
  352.         changed_cline_bef_curs();        /* recompute curosr pos partly */
  353.     approximate_botline();
  354.     }
  355.     curbuf->b_p_ts = new_ts;
  356.     coladvance(curwin->w_curswant);
  357.  
  358.     u_clearline();
  359.     update_screen(NOT_VALID);
  360. }
  361. #endif
  362.  
  363. /*
  364.  * :move command - move lines line1-line2 to line dest
  365.  *
  366.  * return FAIL for failure, OK otherwise
  367.  */
  368.     int
  369. do_move(line1, line2, dest)
  370.     linenr_t    line1;
  371.     linenr_t    line2;
  372.     linenr_t    dest;
  373. {
  374.     char_u    *str;
  375.     linenr_t    l;
  376.     linenr_t    extra;        /* Num lines added before line1 */
  377.     linenr_t    num_lines;  /* Num lines moved */
  378.     linenr_t    last_line;  /* Last line in file after adding new text */
  379.  
  380.     if (dest >= line1 && dest < line2)
  381.     {
  382.     EMSG("Move lines into themselves");
  383.     return FAIL;
  384.     }
  385.  
  386.     num_lines = line2 - line1 + 1;
  387.  
  388.     /*
  389.      * First we copy the old text to its new location -- webb
  390.      * Also copy the flag that ":global" command uses.
  391.      */
  392.     if (u_save(dest, dest + 1) == FAIL)
  393.     return FAIL;
  394.     for (extra = 0, l = line1; l <= line2; l++)
  395.     {
  396.     str = vim_strsave(ml_get(l + extra));
  397.     if (str != NULL)
  398.     {
  399.         ml_append(dest + l - line1, str, (colnr_t)0, FALSE);
  400.         vim_free(str);
  401.         if (dest < line1)
  402.         extra++;
  403.     }
  404.     }
  405.  
  406.     /*
  407.      * Now we must be careful adjusting our marks so that we don't overlap our
  408.      * mark_adjust() calls.
  409.      *
  410.      * We adjust the marks within the old text so that they refer to the
  411.      * last lines of the file (temporarily), because we know no other marks
  412.      * will be set there since these line numbers did not exist until we added
  413.      * our new lines.
  414.      *
  415.      * Then we adjust the marks on lines between the old and new text positions
  416.      * (either forwards or backwards).
  417.      *
  418.      * And Finally we adjust the marks we put at the end of the file back to
  419.      * their final destination at the new text position -- webb
  420.      */
  421.     last_line = curbuf->b_ml.ml_line_count;
  422.     mark_adjust(line1, line2, last_line - line2, 0L);
  423.     if (dest >= line2)
  424.     {
  425.     mark_adjust(line2 + 1, dest, -num_lines, 0L);
  426.     curbuf->b_op_start.lnum = dest - num_lines + 1;
  427.     curbuf->b_op_end.lnum = dest;
  428.     }
  429.     else
  430.     {
  431.     mark_adjust(dest + 1, line1 - 1, num_lines, 0L);
  432.     curbuf->b_op_start.lnum = dest + 1;
  433.     curbuf->b_op_end.lnum = dest + num_lines;
  434.     }
  435.     curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
  436.     mark_adjust(last_line - num_lines + 1, last_line,
  437.                          -(last_line - dest - extra), 0L);
  438.  
  439.     /*
  440.      * Now we delete the original text -- webb
  441.      */
  442.     if (u_save(line1 + extra - 1, line2 + extra + 1) == FAIL)
  443.     return FAIL;
  444.  
  445.     for (l = line1; l <= line2; l++)
  446.     ml_delete(line1 + extra, TRUE);
  447.  
  448.     CHANGED;
  449.     if (!global_busy && num_lines > p_report)
  450.     smsg((char_u *)"%ld line%s moved", num_lines, plural(num_lines));
  451.  
  452.     /*
  453.      * Leave the cursor on the last of the moved lines.
  454.      */
  455.     if (dest >= line1)
  456.     curwin->w_cursor.lnum = dest;
  457.     else
  458.     curwin->w_cursor.lnum = dest + (line2 - line1) + 1;
  459.     changed_line_abv_curs();
  460.     /*
  461.      * TODO: should recompute w_botline for simple situations.
  462.      */
  463.     invalidate_botline();
  464.     return OK;
  465. }
  466.  
  467. /*
  468.  * :copy command - copy lines line1-line2 to line n
  469.  */
  470.     void
  471. do_copy(line1, line2, n)
  472.     linenr_t    line1;
  473.     linenr_t    line2;
  474.     linenr_t    n;
  475. {
  476.     linenr_t        lnum;
  477.     char_u        *p;
  478.  
  479.     lnum = line2 - line1 + 1;
  480.     mark_adjust(n + 1, (linenr_t)MAXLNUM, lnum, 0L);
  481.     curbuf->b_op_start.lnum = n + 1;
  482.     curbuf->b_op_end.lnum = n + lnum;
  483.     curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
  484.  
  485.     /*
  486.      * there are three situations:
  487.      * 1. destination is above line1
  488.      * 2. destination is between line1 and line2
  489.      * 3. destination is below line2
  490.      *
  491.      * n = destination (when starting)
  492.      * curwin->w_cursor.lnum = destination (while copying)
  493.      * line1 = start of source (while copying)
  494.      * line2 = end of source (while copying)
  495.      */
  496.     if (u_save(n, n + 1) == FAIL)
  497.     return;
  498.     curwin->w_cursor.lnum = n;
  499.     while (line1 <= line2)
  500.     {
  501.     /* need to use vim_strsave() because the line will be unlocked
  502.         within ml_append */
  503.     p = vim_strsave(ml_get(line1));
  504.     if (p != NULL)
  505.     {
  506.         ml_append(curwin->w_cursor.lnum, p, (colnr_t)0, FALSE);
  507.         vim_free(p);
  508.     }
  509.         /* situation 2: skip already copied lines */
  510.     if (line1 == n)
  511.         line1 = curwin->w_cursor.lnum;
  512.     ++line1;
  513.     if (curwin->w_cursor.lnum < line1)
  514.         ++line1;
  515.     if (curwin->w_cursor.lnum < line2)
  516.         ++line2;
  517.     ++curwin->w_cursor.lnum;
  518.     }
  519.     CHANGED;
  520.     changed_line_abv_curs();
  521.     /*
  522.      * TODO: should recompute w_botline for simple situations.
  523.      */
  524.     invalidate_botline();
  525.     msgmore((long)lnum);
  526. }
  527.  
  528. /*
  529.  * Handle the ":!cmd" command.    Also for ":r !cmd" and ":w !cmd"
  530.  * Bangs in the argument are replaced with the previously entered command.
  531.  * Remember the argument.
  532.  */
  533.     void
  534. do_bang(addr_count, line1, line2, forceit, arg, do_in, do_out)
  535.     int        addr_count;
  536.     linenr_t    line1, line2;
  537.     int        forceit;
  538.     char_u    *arg;
  539.     int        do_in, do_out;
  540. {
  541.     static  char_u  *prevcmd = NULL;        /* the previous command */
  542.     char_u        *newcmd = NULL;        /* the new command */
  543.     int            free_newcmd = FALSE;    /* need to free() newcmd */
  544.     int            ins_prevcmd;
  545.     char_u        *t;
  546.     char_u        *p;
  547.     char_u        *trailarg;
  548.     int            len;
  549.     int            scroll_save = msg_scroll;
  550.  
  551.     /*
  552.      * Disallow shell commands for "rvim".
  553.      * Disallow shell commands from .exrc and .vimrc in current directory for
  554.      * security reasons.
  555.      */
  556.     if (check_restricted() || check_secure())
  557.     return;
  558.  
  559.     if (addr_count == 0)        /* :! */
  560.     {
  561.     msg_scroll = FALSE;        /* don't scroll here */
  562.     autowrite_all();
  563.     msg_scroll = scroll_save;
  564.     }
  565.  
  566.     /*
  567.      * Try to find an embedded bang, like in :!<cmd> ! [args]
  568.      * (:!! is indicated by the 'forceit' variable)
  569.      */
  570.     ins_prevcmd = forceit;
  571.     trailarg = arg;
  572.     do
  573.     {
  574.     len = STRLEN(trailarg) + 1;
  575.     if (newcmd != NULL)
  576.         len += STRLEN(newcmd);
  577.     if (ins_prevcmd)
  578.     {
  579.         if (prevcmd == NULL)
  580.         {
  581.         emsg(e_noprev);
  582.         vim_free(newcmd);
  583.         return;
  584.         }
  585.         len += STRLEN(prevcmd);
  586.     }
  587.     if ((t = alloc(len)) == NULL)
  588.     {
  589.         vim_free(newcmd);
  590.         return;
  591.     }
  592.     *t = NUL;
  593.     if (newcmd != NULL)
  594.         STRCAT(t, newcmd);
  595.     if (ins_prevcmd)
  596.         STRCAT(t, prevcmd);
  597.     p = t + STRLEN(t);
  598.     STRCAT(t, trailarg);
  599.     vim_free(newcmd);
  600.     newcmd = t;
  601.  
  602.     /*
  603.      * Scan the rest of the argument for '!', which is replaced by the
  604.      * previous command.  "\!" is replaced by "!" (this is vi compatible).
  605.      */
  606.     trailarg = NULL;
  607.     while (*p)
  608.     {
  609.         if (*p == '!')
  610.         {
  611.         if (p > newcmd && p[-1] == '\\')
  612.             vim_memmove(p - 1, p, (size_t)(STRLEN(p) + 1));
  613.         else
  614.         {
  615.             trailarg = p;
  616.             *trailarg++ = NUL;
  617.             ins_prevcmd = TRUE;
  618.             break;
  619.         }
  620.         }
  621.         ++p;
  622.     }
  623.     } while (trailarg != NULL);
  624.  
  625.     vim_free(prevcmd);
  626.     prevcmd = newcmd;
  627.  
  628.     if (bangredo)        /* put cmd in redo buffer for ! command */
  629.     {
  630.     AppendToRedobuff(prevcmd);
  631.     AppendToRedobuff((char_u *)"\n");
  632.     bangredo = FALSE;
  633.     }
  634.     /*
  635.      * Add quotes around the command, for shells that need them.
  636.      */
  637.     if (*p_shq != NUL)
  638.     {
  639.     newcmd = alloc((unsigned)(STRLEN(prevcmd) + 2 * STRLEN(p_shq) + 1));
  640.     if (newcmd == NULL)
  641.         return;
  642.     STRCPY(newcmd, p_shq);
  643.     STRCAT(newcmd, prevcmd);
  644.     STRCAT(newcmd, p_shq);
  645.     free_newcmd = TRUE;
  646.     }
  647.     if (addr_count == 0)        /* :! */
  648.     {
  649.         /* echo the command */
  650.     msg_start();
  651.     msg_putchar(':');
  652.     msg_putchar('!');
  653.     msg_outtrans(newcmd);
  654.     msg_clr_eos();
  655.     windgoto(msg_row, msg_col);
  656.  
  657.     do_shell(newcmd, 0);
  658.     }
  659.     else                /* :range! */
  660.     /* Careful: This may recursively call do_bang() again! (because of
  661.      * autocommands) */
  662.     do_filter(line1, line2, newcmd, do_in, do_out);
  663.     if (free_newcmd)
  664.     vim_free(newcmd);
  665. }
  666.  
  667. /*
  668.  * call a shell to execute a command
  669.  */
  670.     void
  671. do_shell(cmd, flags)
  672.     char_u  *cmd;
  673.     int        flags;    /* may be SHELL_DOOUT when output is redirected */
  674. {
  675.     BUF        *buf;
  676. #ifndef USE_GUI_WIN32
  677.     int        save_nwr;
  678. #endif
  679.  
  680.     /*
  681.      * Disallow shell commands for "rvim".
  682.      * Disallow shell commands from .exrc and .vimrc in current directory for
  683.      * security reasons.
  684.      */
  685.     if (check_restricted() || check_secure())
  686.     {
  687.     msg_end();
  688.     return;
  689.     }
  690.  
  691. #ifdef WIN32
  692.     /*
  693.      * Check if external commands are allowed now.
  694.      */
  695.     if (can_end_termcap_mode(TRUE) == FALSE)
  696.     return;
  697. #endif
  698.  
  699.     /*
  700.      * For autocommands we want to get the output on the current screen, to
  701.      * avoid having to type return below.
  702.      */
  703.     msg_putchar('\r');            /* put cursor at start of line */
  704. #ifdef AUTOCMD
  705.     if (!autocmd_busy)
  706. #endif
  707.     stoptermcap();
  708.     msg_putchar('\n');            /* may shift screen one line up */
  709.  
  710.     /* warning message before calling the shell */
  711.     if (p_warn
  712. #ifdef AUTOCMD
  713.         && !autocmd_busy
  714. #endif
  715.                    )
  716.     for (buf = firstbuf; buf; buf = buf->b_next)
  717.         if (buf_changed(buf))
  718.         {
  719. #ifdef USE_GUI_WIN32
  720.         starttermcap();        /* don't want a message box here */
  721. #endif
  722.         MSG_PUTS("[No write since last change]\n");
  723. #ifdef USE_GUI_WIN32
  724.         stoptermcap();
  725. #endif
  726.         break;
  727.         }
  728.  
  729. /* This windgoto is required for when the '\n' resulted in a "delete line 1"
  730.  * command to the terminal. */
  731.  
  732.     if (!swapping_screen())
  733.     windgoto(msg_row, msg_col);
  734.     cursor_on();
  735.     (void)call_shell(cmd, SHELL_COOKED | flags);
  736.     need_check_timestamps = TRUE;
  737.  
  738. /*
  739.  * put the message cursor at the end of the screen, avoids wait_return() to
  740.  * overwrite the text that the external command showed
  741.  */
  742.     if (!swapping_screen())
  743.     {
  744.     msg_row = Rows - 1;
  745.     msg_col = 0;
  746.     }
  747.  
  748. #ifdef AUTOCMD
  749.     if (autocmd_busy)
  750.     must_redraw = CLEAR;
  751.     else
  752. #endif
  753.     {
  754.     /*
  755.      * For ":sh" there is no need to call wait_return(), just redraw.
  756.      * Also for the Win32 GUI (the output is in a console window).
  757.      * Otherwise there is probably text on the screen that the user wants
  758.      * to read before redrawing, so call wait_return().
  759.      */
  760. #ifndef USE_GUI_WIN32
  761.     if (cmd == NULL)
  762.     {
  763. #endif
  764.         must_redraw = CLEAR;
  765.         need_wait_return = FALSE;
  766.         dont_wait_return = TRUE;
  767. #ifndef USE_GUI_WIN32
  768.     }
  769.     else
  770.     {
  771.         /*
  772.          * If we switch screens when starttermcap() is called, we really
  773.          * want to wait for "hit return to continue".
  774.          */
  775.         save_nwr = no_wait_return;
  776.         if (swapping_screen())
  777.         no_wait_return = FALSE;
  778. #ifdef AMIGA
  779.         wait_return(term_console ? -1 : TRUE);    /* see below */
  780. #else
  781.         wait_return(TRUE);
  782. #endif
  783.         no_wait_return = save_nwr;
  784.     }
  785. #endif /* USE_GUI_WIN32 */
  786.     starttermcap();        /* start termcap if not done by wait_return() */
  787.  
  788.     /*
  789.      * In an Amiga window redrawing is caused by asking the window size.
  790.      * If we got an interrupt this will not work. The chance that the
  791.      * window size is wrong is very small, but we need to redraw the
  792.      * screen.  Don't do this if ':' hit in wait_return().    THIS IS UGLY
  793.      * but it saves an extra redraw.
  794.      */
  795. #ifdef AMIGA
  796.     if (skip_redraw)        /* ':' hit in wait_return() */
  797.         must_redraw = CLEAR;
  798.     else if (term_console)
  799.     {
  800.         OUT_STR("\033[0 q");    /* get window size */
  801.         if (got_int)
  802.         must_redraw = CLEAR;    /* if got_int is TRUE, redraw needed */
  803.         else
  804.         must_redraw = 0;    /* no extra redraw needed */
  805.     }
  806. #endif /* AMIGA */
  807.     }
  808.  
  809.     /* display any error messages now */
  810.     mch_display_error();
  811. }
  812.  
  813. /*
  814.  * do_filter: filter lines through a command given by the user
  815.  *
  816.  * We use temp files and the call_shell() routine here. This would normally
  817.  * be done using pipes on a UNIX machine, but this is more portable to
  818.  * non-unix machines. The call_shell() routine needs to be able
  819.  * to deal with redirection somehow, and should handle things like looking
  820.  * at the PATH env. variable, and adding reasonable extensions to the
  821.  * command name given by the user. All reasonable versions of call_shell()
  822.  * do this.
  823.  * We use input redirection if do_in is TRUE.
  824.  * We use output redirection if do_out is TRUE.
  825.  */
  826.     static void
  827. do_filter(line1, line2, buff, do_in, do_out)
  828.     linenr_t    line1, line2;
  829.     char_u    *buff;
  830.     int        do_in, do_out;
  831. {
  832.     char_u    *itmp = NULL;
  833.     char_u    *otmp = NULL;
  834.     linenr_t    linecount;
  835.     FPOS    cursor_save;
  836. #ifdef AUTOCMD
  837.     BUF        *old_curbuf = curbuf;
  838. #endif
  839.  
  840.     if (*buff == NUL)        /* no filter command */
  841.     return;
  842.  
  843. #ifdef WIN32
  844.     /*
  845.      * Check if external commands are allowed now.
  846.      */
  847.     if (can_end_termcap_mode(TRUE) == FALSE)
  848.     return;
  849. #endif
  850.  
  851.     cursor_save = curwin->w_cursor;
  852.     linecount = line2 - line1 + 1;
  853.     curwin->w_cursor.lnum = line1;
  854.     curwin->w_cursor.col = 0;
  855.     changed_line_abv_curs();
  856.     invalidate_botline();
  857.  
  858.     /*
  859.      * 1. Form temp file names
  860.      * 2. Write the lines to a temp file
  861.      * 3. Run the filter command on the temp file
  862.      * 4. Read the output of the command into the buffer
  863.      * 5. Delete the original lines to be filtered
  864.      * 6. Remove the temp files
  865.      */
  866.  
  867.     if ((do_in && (itmp = vim_tempname('i')) == NULL) ||
  868.                    (do_out && (otmp = vim_tempname('o')) == NULL))
  869.     {
  870.     emsg(e_notmp);
  871.     goto filterend;
  872.     }
  873.  
  874. /*
  875.  * The writing and reading of temp files will not be shown.
  876.  * Vi also doesn't do this and the messages are not very informative.
  877.  */
  878.     ++no_wait_return;        /* don't call wait_return() while busy */
  879.     if (do_in && buf_write(curbuf, itmp, NULL, line1, line2,
  880.                        FALSE, FALSE, FALSE, TRUE) == FAIL)
  881.     {
  882.     msg_putchar('\n');            /* keep message from buf_write() */
  883.     --no_wait_return;
  884.     (void)emsg2(e_notcreate, itmp);        /* will call wait_return */
  885.     goto filterend;
  886.     }
  887. #ifdef AUTOCMD
  888.     if (curbuf != old_curbuf)
  889.     goto filterend;
  890. #endif
  891.  
  892.     if (!do_out)
  893.     msg_putchar('\n');
  894.  
  895. #if (defined(UNIX) && !defined(ARCHIE)) || defined(OS2)
  896. /*
  897.  * put braces around the command (for concatenated commands)
  898.  */
  899.     sprintf((char *)IObuff, "(%s)", (char *)buff);
  900.     if (do_in)
  901.     {
  902.     STRCAT(IObuff, " < ");
  903.     STRCAT(IObuff, itmp);
  904.     }
  905. #else
  906. /*
  907.  * for shells that don't understand braces around commands, at least allow
  908.  * the use of commands in a pipe.
  909.  */
  910.     STRCPY(IObuff, buff);
  911.     if (do_in)
  912.     {
  913.     char_u        *p;
  914.  
  915.     /*
  916.      * If there is a pipe, we have to put the '<' in front of it.
  917.      * Don't do this when 'shellquote' is not empty, otherwise the
  918.      * redirection would be inside the quotes.
  919.      */
  920.     p = vim_strchr(IObuff, '|');
  921.     if (p && *p_shq == NUL)
  922.         *p = NUL;
  923.     STRCAT(IObuff, " <");        /* " < " causes problems on Amiga */
  924.     STRCAT(IObuff, itmp);
  925.     p = vim_strchr(buff, '|');
  926.     if (p && *p_shq == NUL)
  927.         STRCAT(IObuff, p);
  928.     }
  929. #endif
  930.     if (do_out)
  931.     {
  932.     char_u *p;
  933.  
  934.     if ((p = vim_strchr(p_srr, '%')) != NULL && p[1] == 's')
  935.     {
  936.         p = IObuff + STRLEN(IObuff);
  937.         *p++ = ' '; /* not really needed? Not with sh, ksh or bash */
  938.         sprintf((char *)p, (char *)p_srr, (char *)otmp);
  939.     }
  940.     else
  941.                 /* " %s %s" causes problems on Amiga */
  942.         sprintf((char *)IObuff + STRLEN(IObuff), " %s%s",
  943.                          (char *)p_srr, (char *)otmp);
  944.     }
  945.  
  946.     windgoto((int)Rows - 1, 0);
  947.     cursor_on();
  948.  
  949.     /*
  950.      * When not redirecting the output the command can write anything to the
  951.      * screen. If 'shellredir' is equal to ">", screen may be messed up by
  952.      * stderr output of external command. Clear the screen later.
  953.      * If do_in is FALSE, this could be something like ":r !cat", which may
  954.      * also mess up the screen, clear it later.
  955.      */
  956.     if (!do_out || STRCMP(p_srr, ">") == 0 || !do_in)
  957.     must_redraw = CLEAR;
  958.     else
  959.     redraw_later(NOT_VALID);
  960.  
  961.     /*
  962.      * When call_shell() fails wait_return() is called to give the user a
  963.      * chance to read the error messages. Otherwise errors are ignored, so you
  964.      * can see the error messages from the command that appear on stdout; use
  965.      * 'u' to fix the text
  966.      * Switch to cooked mode when not redirecting stdin, avoids that something
  967.      * like ":r !cat" hangs.
  968.      * Pass on the SHELL_DOOUT flag when the output is being redirected.
  969.      */
  970.     if (call_shell(IObuff, SHELL_FILTER | SHELL_COOKED |
  971.                       (do_out ? SHELL_DOOUT : 0)) == FAIL)
  972.     {
  973.     must_redraw = CLEAR;
  974.     wait_return(FALSE);
  975.     }
  976.  
  977. #ifdef USE_GUI_WIN32
  978.     /*
  979.      * Under win32s, the shell returns before the command is finished, so we
  980.      * must wait in order to process results.  TODO: find a real solution!
  981.      */
  982.     if (gui_is_win32s())
  983.     {
  984.     int    no_wait_return_save = no_wait_return;
  985.  
  986.     no_wait_return = FALSE;
  987.     MSG_PUTS("\nWaiting for DOS completion....\n");
  988.     wait_return(FALSE);
  989.     no_wait_return = no_wait_return_save;
  990.     }
  991. #endif
  992.  
  993.     need_check_timestamps = TRUE;
  994.  
  995.     if (do_out)
  996.     {
  997.     if (u_save((linenr_t)(line2), (linenr_t)(line2 + 1)) == FAIL)
  998.     {
  999.         goto error;
  1000.     }
  1001.     if (readfile(otmp, NULL, line2, (linenr_t)0, (linenr_t)MAXLNUM,
  1002.                              READ_FILTER) == FAIL)
  1003.     {
  1004.         msg_putchar('\n');
  1005.         emsg2(e_notread, otmp);
  1006.         goto error;
  1007.     }
  1008. #ifdef AUTOCMD
  1009.     if (curbuf != old_curbuf)
  1010.         goto filterend;
  1011. #endif
  1012.  
  1013.     if (do_in)
  1014.     {
  1015.         /*
  1016.          * Put cursor on first filtered line for ":range!cmd".
  1017.          * Adjust '[ and '] (set by buf_write()).
  1018.          */
  1019.         curwin->w_cursor.lnum = line1;
  1020.         del_lines(linecount, TRUE, TRUE);
  1021.         curbuf->b_op_start.lnum -= linecount;    /* adjust '[ */
  1022.         curbuf->b_op_end.lnum -= linecount;        /* adjust '] */
  1023.         write_lnum_adjust(-linecount);        /* adjust last line
  1024.                                for next write */
  1025.     }
  1026.     else
  1027.     {
  1028.         /*
  1029.          * Put cursor on last new line for ":r !cmd".
  1030.          */
  1031.         curwin->w_cursor.lnum = curbuf->b_op_end.lnum;
  1032.         linecount = curbuf->b_op_end.lnum - curbuf->b_op_start.lnum + 1;
  1033.     }
  1034.     beginline(BL_WHITE | BL_FIX);        /* cursor on first non-blank */
  1035.     --no_wait_return;
  1036.  
  1037.     if (linecount > p_report)
  1038.     {
  1039.         if (do_in)
  1040.         {
  1041.         sprintf((char *)msg_buf, "%ld lines filtered", (long)linecount);
  1042.         if (msg(msg_buf) && !msg_scroll)
  1043.         {
  1044.             keep_msg = msg_buf;        /* display message after redraw */
  1045.             keep_msg_attr = 0;
  1046.         }
  1047.         }
  1048.         else
  1049.         msgmore((long)linecount);
  1050.     }
  1051.     }
  1052.     else
  1053.     {
  1054. error:
  1055.     /* put cursor back in same position for ":w !cmd" */
  1056.     curwin->w_cursor = cursor_save;
  1057.     --no_wait_return;
  1058.     wait_return(FALSE);
  1059.     }
  1060.  
  1061. filterend:
  1062.  
  1063. #ifdef AUTOCMD
  1064.     if (curbuf != old_curbuf)
  1065.     {
  1066.     --no_wait_return;
  1067.     EMSG("*Filter* Autocommands must not change current buffer");
  1068.     }
  1069. #endif
  1070.     if (itmp != NULL)
  1071.     vim_remove(itmp);
  1072.     if (otmp != NULL)
  1073.     vim_remove(otmp);
  1074.     vim_free(itmp);
  1075.     vim_free(otmp);
  1076. }
  1077.  
  1078. #ifdef VIMINFO
  1079.  
  1080. static int no_viminfo __ARGS((void));
  1081. static int  viminfo_errcnt;
  1082.  
  1083.     static int
  1084. no_viminfo()
  1085. {
  1086.     /* "vim -i NONE" does not read or write a viminfo file */
  1087.     return (use_viminfo != NULL && STRCMP(use_viminfo, "NONE") == 0);
  1088. }
  1089.  
  1090. /*
  1091.  * Report an error for reading a viminfo file.
  1092.  * Count the number of errors.    When there are more than 10, return TRUE.
  1093.  */
  1094.     int
  1095. viminfo_error(message, line)
  1096.     char    *message;
  1097.     char_u  *line;
  1098. {
  1099.     sprintf((char *)IObuff, "viminfo: %s in line: ", message);
  1100.     STRNCAT(IObuff, line, IOSIZE - STRLEN(IObuff));
  1101.     emsg(IObuff);
  1102.     if (++viminfo_errcnt >= 10)
  1103.     {
  1104.     EMSG("viminfo: Too many errors, skipping rest of file");
  1105.     return TRUE;
  1106.     }
  1107.     return FALSE;
  1108. }
  1109.  
  1110. /*
  1111.  * read_viminfo() -- Read the viminfo file.  Registers etc. which are already
  1112.  * set are not over-written unless force is TRUE. -- webb
  1113.  */
  1114.     int
  1115. read_viminfo(file, want_info, want_marks, forceit)
  1116.     char_u  *file;
  1117.     int        want_info;
  1118.     int        want_marks;
  1119.     int        forceit;
  1120. {
  1121.     FILE    *fp;
  1122.  
  1123.     if (no_viminfo())
  1124.     return FAIL;
  1125.  
  1126.     file = viminfo_filename(file);        /* may set to default if NULL */
  1127.     if ((fp = fopen((char *)file, READBIN)) == NULL)
  1128.     return FAIL;
  1129.  
  1130.     if (p_verbose > 0)
  1131.     smsg((char_u *)"Reading viminfo file \"%s\"%s%s", file,
  1132.             want_info ? " info" : "",
  1133.             want_marks ? " marks" : "");
  1134.  
  1135.     viminfo_errcnt = 0;
  1136.     do_viminfo(fp, NULL, want_info, want_marks, forceit);
  1137.  
  1138.     fclose(fp);
  1139.  
  1140.     return OK;
  1141. }
  1142.  
  1143. /*
  1144.  * write_viminfo() -- Write the viminfo file.  The old one is read in first so
  1145.  * that effectively a merge of current info and old info is done.  This allows
  1146.  * multiple vims to run simultaneously, without losing any marks etc.  If
  1147.  * forceit is TRUE, then the old file is not read in, and only internal info is
  1148.  * written to the file. -- webb
  1149.  */
  1150.     void
  1151. write_viminfo(file, forceit)
  1152.     char_u  *file;
  1153.     int        forceit;
  1154. {
  1155.     FILE        *fp_in = NULL;    /* input viminfo file, if any */
  1156.     FILE        *fp_out = NULL;    /* output viminfo file */
  1157.     char_u        *tempname = NULL;    /* name of temp viminfo file */
  1158.     struct stat        st_new;        /* stat() of potential new file */
  1159.     char_u        *wp;
  1160. #ifdef UNIX
  1161.     int            shortname = FALSE;    /* use 8.3 file name */
  1162.     mode_t        umask_save;
  1163.     struct stat        st_old;        /* stat() of existing viminfo file */
  1164. #endif
  1165.  
  1166.     if (no_viminfo())
  1167.     return;
  1168.  
  1169.     file = viminfo_filename(file);    /* may set to default if NULL */
  1170.     file = vim_strsave(file);        /* make a copy, don't want NameBuff */
  1171.  
  1172.     if (file != NULL)
  1173.     {
  1174.     fp_in = fopen((char *)file, READBIN);
  1175.     if (fp_in == NULL)
  1176.     {
  1177.         /* if it does exist, but we can't read it, don't try writing */
  1178.         if (stat((char *)file, &st_new) == 0)
  1179.         goto end;
  1180. #ifdef UNIX
  1181.         /*
  1182.          * For Unix we create the .viminfo non-accessible for others,
  1183.          * because it may contain text from non-accessible documents.
  1184.          */
  1185.         umask_save = umask(077);
  1186. #endif
  1187.         fp_out = fopen((char *)file, WRITEBIN);
  1188. #ifdef UNIX
  1189.         (void)umask(umask_save);
  1190. #endif
  1191.     }
  1192.     else
  1193.     {
  1194.         /*
  1195.          * There is an existing viminfo file.  Create a temporary file to
  1196.          * write the new viminfo into, in the same directory as the
  1197.          * existing viminfo file, which will be renamed later.
  1198.          */
  1199. #ifdef UNIX
  1200.         /*
  1201.          * For Unix we check the owner of the file.  It's not very nice to
  1202.          * overwrite a user's viminfo file after a "su root", with a
  1203.          * viminfo file that the user can't read.
  1204.          */
  1205.         st_old.st_dev = st_old.st_ino = 0;
  1206.         st_old.st_mode = 0600;
  1207.         if (stat((char *)file, &st_old) == 0 && getuid() &&
  1208.             !(st_old.st_uid == getuid()
  1209.                 ? (st_old.st_mode & 0200)
  1210.                 : (st_old.st_gid == getgid()
  1211.                     ? (st_old.st_mode & 0020)
  1212.                     : (st_old.st_mode & 0002))))
  1213.         {
  1214.         int    tt;
  1215.  
  1216.         /* avoid a wait_return for this message, it's annoying */
  1217.         tt = msg_didany;
  1218.         EMSG2("Viminfo file is not writable: %s", file);
  1219.         msg_didany = tt;
  1220.         goto end;
  1221.         }
  1222. #endif
  1223.  
  1224.         /*
  1225.          * Make tempname.
  1226.          * May try twice: Once normal and once with shortname set, just in
  1227.          * case somebody puts his viminfo file in an 8.3 filesystem.
  1228.          */
  1229.         for (;;)
  1230.         {
  1231.         tempname = buf_modname(
  1232. #ifdef UNIX
  1233.                     shortname,
  1234. #else
  1235. # ifdef SHORT_FNAME
  1236.                     TRUE,
  1237. # else
  1238. #  ifdef USE_GUI_WIN32
  1239.                     gui_is_win32s(),
  1240. #  else
  1241.                     FALSE,
  1242. #  endif
  1243. # endif
  1244. #endif
  1245.                            file, (char_u *)".tmp", FALSE);
  1246.         if (tempname == NULL)        /* out of memory */
  1247.             break;
  1248.  
  1249.         /*
  1250.          * Check if tempfile already exists.  Never overwrite an
  1251.          * existing file!
  1252.          */
  1253.         if (stat((char *)tempname, &st_new) == 0)
  1254.         {
  1255. #ifdef UNIX
  1256.             /*
  1257.              * Check if tempfile is same as original file.  May happen
  1258.              * when modname gave the same file back.  E.g.  silly
  1259.              * link, or file name-length reached.  Try again with
  1260.              * shortname set.
  1261.              */
  1262.             if (!shortname && st_new.st_dev == st_old.st_dev &&
  1263.                 st_new.st_ino == st_old.st_ino)
  1264.             {
  1265.             vim_free(tempname);
  1266.             tempname = NULL;
  1267.             shortname = TRUE;
  1268.             continue;
  1269.             }
  1270. #endif
  1271.             /*
  1272.              * Try another name.  Change one character, just before
  1273.              * the extension.  This should also work for an 8.3
  1274.              * file name, when after adding the extension it still is
  1275.              * the same file as the original.
  1276.              */
  1277.             wp = tempname + STRLEN(tempname) - 5;
  1278.             if (wp < gettail(tempname))        /* empty file name? */
  1279.             wp = gettail(tempname);
  1280.             for (*wp = 'z'; stat((char *)tempname, &st_new) == 0; --*wp)
  1281.             {
  1282.             /*
  1283.              * They all exist?  Must be something wrong! Don't
  1284.              * write the viminfo file then.
  1285.              */
  1286.             if (*wp == 'a')
  1287.             {
  1288.                 vim_free(tempname);
  1289.                 tempname = NULL;
  1290.                 break;
  1291.             }
  1292.             }
  1293.         }
  1294.         break;
  1295.         }
  1296.  
  1297.         if (tempname != NULL)
  1298.         {
  1299.         fp_out = fopen((char *)tempname, WRITEBIN);
  1300.  
  1301.         /*
  1302.          * If we can't create in the same directory, try creating a
  1303.          * "normal" temp file.
  1304.          */
  1305.         if (fp_out == NULL)
  1306.         {
  1307.             vim_free(tempname);
  1308.             if ((tempname = vim_tempname('o')) != NULL)
  1309.             fp_out = fopen((char *)tempname, WRITEBIN);
  1310.         }
  1311. #ifdef UNIX
  1312.         /*
  1313.          * Set file protection same as original file, but strip s-bit
  1314.          * and make sure the owner can read/write it.
  1315.          */
  1316.         if (fp_out != NULL)
  1317.         {
  1318.             (void)mch_setperm(tempname,
  1319.                       (long)((st_old.st_mode & 0777) | 0600));
  1320.             /* this only works for root: */
  1321.             (void)chown((char *)tempname, st_old.st_uid, st_old.st_gid);
  1322.         }
  1323. #endif
  1324.         }
  1325.     }
  1326.     }
  1327.  
  1328.     /*
  1329.      * Check if the new viminfo file can be written to.
  1330.      */
  1331.     if (file == NULL || fp_out == NULL)
  1332.     {
  1333.     EMSG2("Can't write viminfo file %s!", file == NULL ? (char_u *)"" :
  1334.                           fp_in == NULL ? file : tempname);
  1335.     if (fp_in != NULL)
  1336.         fclose(fp_in);
  1337.     goto end;
  1338.     }
  1339.  
  1340.     if (p_verbose > 0)
  1341.     smsg((char_u *)"Writing viminfo file \"%s\"", file);
  1342.  
  1343.     viminfo_errcnt = 0;
  1344.     do_viminfo(fp_in, fp_out, !forceit, !forceit, FALSE);
  1345.  
  1346.     fclose(fp_out);        /* errors are ignored !? */
  1347.     if (fp_in != NULL)
  1348.     {
  1349.     fclose(fp_in);
  1350.     /*
  1351.      * In case of an error, don't overwrite the original viminfo file.
  1352.      */
  1353.     if (viminfo_errcnt || vim_rename(tempname, file) == -1)
  1354.         vim_remove(tempname);
  1355.     }
  1356. end:
  1357.     vim_free(file);
  1358.     vim_free(tempname);
  1359. }
  1360.  
  1361. /*
  1362.  * Get the viminfo file name to use.
  1363.  * If "file" is given and not empty, use it (has already been expanded by
  1364.  * cmdline functions).
  1365.  * Otherwise use "-i file_name", value from 'viminfo' or the default, and
  1366.  * expand environment variables.
  1367.  */
  1368.     static char_u *
  1369. viminfo_filename(file)
  1370.     char_u    *file;
  1371. {
  1372.     if (file == NULL || *file == NUL)
  1373.     {
  1374.     if (use_viminfo != NULL)
  1375.         file = use_viminfo;
  1376.     else if ((file = find_viminfo_parameter('n')) == NULL || *file == NUL)
  1377.     {
  1378. #ifdef VIMINFO_FILE2
  1379.         /* don't use $HOME when not defined (turned into "c:/"!). */
  1380.         if (vim_getenv((char_u *)"HOME") == NULL)
  1381.         {
  1382.         /* don't use $VIM when not available. */
  1383.         expand_env((char_u *)"$VIM", NameBuff, MAXPATHL);
  1384.         if (STRCMP("$VIM", NameBuff) != 0)  /* $VIM was expanded */
  1385.             file = (char_u *)VIMINFO_FILE2;
  1386.         else
  1387.             file = (char_u *)VIMINFO_FILE;
  1388.         }
  1389.         else
  1390. #endif
  1391.         file = (char_u *)VIMINFO_FILE;
  1392.     }
  1393.     expand_env(file, NameBuff, MAXPATHL);
  1394.     return NameBuff;
  1395.     }
  1396.     return file;
  1397. }
  1398.  
  1399. /*
  1400.  * do_viminfo() -- Should only be called from read_viminfo() & write_viminfo().
  1401.  */
  1402.     static void
  1403. do_viminfo(fp_in, fp_out, want_info, want_marks, force_read)
  1404.     FILE    *fp_in;
  1405.     FILE    *fp_out;
  1406.     int        want_info;
  1407.     int        want_marks;
  1408.     int        force_read;
  1409. {
  1410.     int        count = 0;
  1411.     int        eof = FALSE;
  1412.     char_u  *line;
  1413.  
  1414.     if ((line = alloc(LSIZE)) == NULL)
  1415.     return;
  1416.  
  1417.     if (fp_in != NULL)
  1418.     {
  1419.     if (want_info)
  1420.         eof = read_viminfo_up_to_marks(line, fp_in, force_read,
  1421.                                   fp_out != NULL);
  1422.     else
  1423.         /* Skip info, find start of marks */
  1424.         while (!(eof = vim_fgets(line, LSIZE, fp_in)) && line[0] != '>')
  1425.         ;
  1426.     }
  1427.     if (fp_out != NULL)
  1428.     {
  1429.     /* Write the info: */
  1430.     fprintf(fp_out, "# This viminfo file was generated by vim\n");
  1431.     fprintf(fp_out, "# You may edit it if you're careful!\n\n");
  1432.     write_viminfo_search_pattern(fp_out);
  1433.     write_viminfo_sub_string(fp_out);
  1434.     write_viminfo_history(fp_out);
  1435.     write_viminfo_registers(fp_out);
  1436.     write_viminfo_filemarks(fp_out);
  1437.     write_viminfo_bufferlist(fp_out);
  1438.     count = write_viminfo_marks(fp_out);
  1439.     }
  1440.     if (fp_in != NULL && want_marks)
  1441.     copy_viminfo_marks(line, fp_in, fp_out, count, eof);
  1442.     vim_free(line);
  1443. }
  1444.  
  1445. /*
  1446.  * read_viminfo_up_to_marks() -- Only called from do_viminfo().  Reads in the
  1447.  * first part of the viminfo file which contains everything but the marks that
  1448.  * are local to a file.  Returns TRUE when end-of-file is reached. -- webb
  1449.  */
  1450.     static int
  1451. read_viminfo_up_to_marks(line, fp, forceit, writing)
  1452.     char_u  *line;
  1453.     FILE    *fp;
  1454.     int        forceit;
  1455.     int        writing;
  1456. {
  1457.     int        eof;
  1458.  
  1459.     prepare_viminfo_history(forceit ? 9999 : 0);
  1460.     eof = vim_fgets(line, LSIZE, fp);
  1461.     while (!eof && line[0] != '>')
  1462.     {
  1463.     switch (line[0])
  1464.     {
  1465.         /* Characters reserved for future expansion, ignored now */
  1466.         case '+': /* "+40 /path/dir file", for running vim without args */
  1467.         case '|': /* to be defined */
  1468.         case '-': /* to be defined */
  1469.         case '!': /* to be defined */
  1470.         case '@': /* to be defined */
  1471.         case '^': /* to be defined */
  1472.         case '*': /* to be defined */
  1473.         /* A comment */
  1474.         case NUL:
  1475.         case '\r':
  1476.         case '\n':
  1477.         case '#':
  1478.         eof = vim_fgets(line, LSIZE, fp);
  1479.         break;
  1480.         case '%': /* entry for buffer list */
  1481.         eof = read_viminfo_bufferlist(line, fp, writing);
  1482.         break;
  1483.         case '"':
  1484.         eof = read_viminfo_register(line, fp, forceit);
  1485.         break;
  1486.         case '/':        /* Search string */
  1487.         case '&':        /* Substitute search string */
  1488.         case '~':        /* Last search string, followed by '/' or '&' */
  1489.         eof = read_viminfo_search_pattern(line, fp, forceit);
  1490.         break;
  1491.         case '$':
  1492.         eof = read_viminfo_sub_string(line, fp, forceit);
  1493.         break;
  1494.         case ':':
  1495.         case '?':
  1496.         case '=':
  1497.         eof = read_viminfo_history(line, fp);
  1498.         break;
  1499.         case '\'':
  1500.         /* How do we have a file mark when the file is not in the
  1501.          * buffer list?
  1502.          */
  1503.         eof = read_viminfo_filemark(line, fp, forceit);
  1504.         break;
  1505.         default:
  1506.         if (viminfo_error("Illegal starting char", line))
  1507.             eof = TRUE;
  1508.         else
  1509.             eof = vim_fgets(line, LSIZE, fp);
  1510.         break;
  1511.     }
  1512.     }
  1513.     finish_viminfo_history();
  1514.     return eof;
  1515. }
  1516.  
  1517. /*
  1518.  * check string read from viminfo file
  1519.  * remove '\n' at the end of the line
  1520.  * - replace CTRL-V CTRL-V with CTRL-V
  1521.  * - replace CTRL-V 'n'    with '\n'
  1522.  */
  1523.     void
  1524. viminfo_readstring(p)
  1525.     char_u    *p;
  1526. {
  1527.     while (*p != NUL && *p != '\n')
  1528.     {
  1529.     if (*p == Ctrl('V'))
  1530.     {
  1531.         if (p[1] == 'n')
  1532.         p[0] = '\n';
  1533.         vim_memmove(p + 1, p + 2, STRLEN(p));
  1534.     }
  1535.     ++p;
  1536.     }
  1537.     *p = NUL;
  1538. }
  1539.  
  1540. /*
  1541.  * write string to viminfo file
  1542.  * - replace CTRL-V with CTRL-V CTRL-V
  1543.  * - replace '\n'   with CTRL-V 'n'
  1544.  * - add a '\n' at the end
  1545.  */
  1546.     void
  1547. viminfo_writestring(fd, p)
  1548.     FILE    *fd;
  1549.     char_u  *p;
  1550. {
  1551.     int        c;
  1552.  
  1553.     while ((c = *p++) != NUL)
  1554.     {
  1555.     if (c == Ctrl('V') || c == '\n')
  1556.     {
  1557.         putc(Ctrl('V'), fd);
  1558.         if (c == '\n')
  1559.         c = 'n';
  1560.     }
  1561.     putc(c, fd);
  1562.     }
  1563.     putc('\n', fd);
  1564. }
  1565. #endif /* VIMINFO */
  1566.  
  1567. /*
  1568.  * Implementation of ":fixdel", also used by get_stty().
  1569.  *  <BS>    resulting <Del>
  1570.  *   ^?        ^H
  1571.  * not ^?    ^?
  1572.  */
  1573.     void
  1574. do_fixdel()
  1575. {
  1576.     char_u  *p;
  1577.  
  1578.     p = find_termcode((char_u *)"kb");
  1579.     add_termcode((char_u *)"kD", p != NULL && *p == 0x7f ?
  1580.                      (char_u *)"\010" : (char_u *)"\177");
  1581. }
  1582.  
  1583.     void
  1584. print_line_no_prefix(lnum, use_number)
  1585.     linenr_t    lnum;
  1586.     int        use_number;
  1587. {
  1588.     char_u    numbuf[20];
  1589.  
  1590.     if (curwin->w_p_nu || use_number)
  1591.     {
  1592.     sprintf((char *)numbuf, "%7ld ", (long)lnum);
  1593.     msg_puts_attr(numbuf, highlight_attr[HLF_N]);    /* Highlight line nrs */
  1594.     }
  1595.     msg_prt_line(ml_get(lnum));
  1596. }
  1597.     void
  1598. print_line(lnum, use_number)
  1599.     linenr_t    lnum;
  1600.     int        use_number;
  1601. {
  1602.     msg_start();
  1603.     print_line_no_prefix(lnum, use_number);
  1604. }
  1605.  
  1606. /*
  1607.  * Implementation of ":file[!] [fname]".
  1608.  */
  1609.     void
  1610. do_file(arg, forceit)
  1611.     char_u  *arg;
  1612.     int        forceit;
  1613. {
  1614.     char_u    *fname, *sfname, *xfname;
  1615.     BUF        *buf;
  1616.  
  1617.     if (*arg != NUL)
  1618.     {
  1619.     /*
  1620.      * The name of the current buffer will be changed.
  1621.      * A new buffer entry needs to be made to hold the old
  1622.      * file name, which will become the alternate file name.
  1623.      */
  1624.     fname = curbuf->b_ffname;
  1625.     sfname = curbuf->b_sfname;
  1626.     xfname = curbuf->b_fname;
  1627.     curbuf->b_ffname = NULL;
  1628.     curbuf->b_sfname = NULL;
  1629.     if (setfname(arg, NULL, TRUE) == FAIL)
  1630.     {
  1631.         curbuf->b_ffname = fname;
  1632.         curbuf->b_sfname = sfname;
  1633.         return;
  1634.     }
  1635.     curbuf->b_notedited = TRUE;
  1636.     buf = buflist_new(fname, xfname, curwin->w_cursor.lnum, FALSE);
  1637.     if (buf != NULL)
  1638.         curwin->w_alt_fnum = buf->b_fnum;
  1639.     vim_free(fname);
  1640.     vim_free(sfname);
  1641.     }
  1642.     /* print full file name if :cd used */
  1643.     fileinfo(FALSE, FALSE, forceit);
  1644. }
  1645.  
  1646.  
  1647. /*
  1648.  * Do the Ex mode :insert and :append commands.
  1649.  * "getline" can be NULL, in which case a line is obtained from the user.
  1650.  */
  1651.     void
  1652. do_append(lnum, getline, cookie)
  1653.     linenr_t    lnum;
  1654.     char_u    *(*getline) __ARGS((int, void *, int));
  1655.     void    *cookie;        /* argument for getline() */
  1656. {
  1657.     char_u    *theline;
  1658.     int        did_undo = FALSE;
  1659.     int        lfirst = lnum;
  1660.  
  1661.     State = INSERT;            /* behave like in Insert mode */
  1662.     while (1)
  1663.     {
  1664.     msg_scroll = TRUE;
  1665.     need_wait_return = FALSE;
  1666.     if (getline == NULL)
  1667.         theline = getcmdline(NUL, 0L, 0);
  1668.     else
  1669.         theline = getline(NUL, cookie, 0);
  1670.     lines_left = Rows - 1;
  1671.     if (theline == NULL || (theline[0] == '.' && theline[1] == NUL))
  1672.         break;
  1673.  
  1674.     if (!did_undo && u_save(lnum, lnum + 1) == FAIL)
  1675.         break;
  1676.     did_undo = TRUE;
  1677.     mark_adjust(lnum + 1, (linenr_t)MAXLNUM, 1L, 0L);
  1678.     ml_append(lnum, theline, (colnr_t)0, FALSE);
  1679.     CHANGED;
  1680.  
  1681.     vim_free(theline);
  1682.     ++lnum;
  1683.     }
  1684.     State = NORMAL;
  1685.  
  1686.     /* "start" is set to lfirst+1 unless that position is invalid (when
  1687.      * lfirst pointed to the end of the buffer and nothig was appended)
  1688.      * "end" is set to lnum when something has been appended, otherwise
  1689.      * it is the same than "start"  -- Acevedo */
  1690.     curbuf->b_op_start.lnum = (lfirst < curbuf->b_ml.ml_line_count) ?
  1691.     lfirst + 1 : curbuf->b_ml.ml_line_count;
  1692.     curbuf->b_op_end.lnum = (lfirst < lnum) ? lnum : curbuf->b_op_start.lnum;
  1693.     curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
  1694.     curwin->w_cursor.lnum = lnum;
  1695.     check_cursor_lnum();
  1696.     beginline(BL_SOL | BL_FIX);
  1697.     changed_line_abv_curs();
  1698.     invalidate_botline();
  1699.  
  1700.     dont_wait_return = TRUE;        /* don't use wait_return() now */
  1701.     need_wait_return = FALSE;
  1702.     update_screen(NOT_VALID);
  1703. }
  1704.  
  1705. /*
  1706.  * Do the Ex mode :change command.
  1707.  * "getline" can be NULL, in which case a line is obtained from the user.
  1708.  */
  1709.     void
  1710. do_change(start, end, getline, cookie)
  1711.     linenr_t    start;
  1712.     linenr_t    end;
  1713.     char_u    *(*getline) __ARGS((int, void *, int));
  1714.     void    *cookie;        /* argument for getline() */
  1715. {
  1716.     if (end >= start && u_save(start - 1, end + 1) == FAIL)
  1717.     return;
  1718.  
  1719.     mark_adjust(start, end, (long)MAXLNUM, (long)(start - end - 1));
  1720.     while (end >= start)
  1721.     {
  1722.     if (curbuf->b_ml.ml_flags & ML_EMPTY)        /* nothing to delete */
  1723.         break;
  1724.     ml_delete(start, FALSE);
  1725.     CHANGED;
  1726.     end--;
  1727.     }
  1728.     do_append(start - 1, getline, cookie);
  1729. }
  1730.  
  1731.     void
  1732. do_z(line, arg)
  1733.     linenr_t    line;
  1734.     char_u    *arg;
  1735. {
  1736.     char_u    *x;
  1737.     int        bigness = curwin->w_height - 3;
  1738.     char_u    kind;
  1739.     int        minus = 0;
  1740.     linenr_t    start, end, curs, i;
  1741.  
  1742.     if (bigness < 1)
  1743.     bigness = 1;
  1744.  
  1745.     x = arg;
  1746.     if (*x == '-' || *x == '+' || *x == '=' || *x == '^' || *x == '.')
  1747.     x++;
  1748.  
  1749.     if (*x != 0)
  1750.     {
  1751.     if (!isdigit(*x))
  1752.     {
  1753.         EMSG("non-numeric argument to :z");
  1754.         return;
  1755.     }
  1756.     else
  1757.         bigness = atoi((char *)x);
  1758.     }
  1759.  
  1760.     kind = *arg;
  1761.  
  1762.     switch (kind)
  1763.     {
  1764.     case '-':
  1765.         start = line - bigness;
  1766.         end = line;
  1767.         curs = line;
  1768.         break;
  1769.  
  1770.     case '=':
  1771.         start = line - bigness / 2 + 1;
  1772.         end = line + bigness / 2 - 1;
  1773.         curs = line;
  1774.         minus = 1;
  1775.         break;
  1776.  
  1777.     case '^':
  1778.         start = line - bigness * 2;
  1779.         end = line - bigness;
  1780.         curs = line - bigness;
  1781.         break;
  1782.  
  1783.     case '.':
  1784.         start = line - bigness / 2;
  1785.         end = line + bigness / 2;
  1786.         curs = end;
  1787.         break;
  1788.  
  1789.     default:  /* '+' */
  1790.         start = line;
  1791.         end = line + bigness;
  1792.         curs = end;
  1793.         break;
  1794.     }
  1795.  
  1796.     if (start < 1)
  1797.     start = 1;
  1798.  
  1799.     if (end > curbuf->b_ml.ml_line_count)
  1800.     end = curbuf->b_ml.ml_line_count;
  1801.  
  1802.     if (curs > curbuf->b_ml.ml_line_count)
  1803.     curs = curbuf->b_ml.ml_line_count;
  1804.  
  1805.     for (i = start; i <= end; i++)
  1806.     {
  1807.     int j;
  1808.  
  1809.     if (minus && (i == line))
  1810.     {
  1811.         msg_putchar('\n');
  1812.  
  1813.         for (j = 1; j < Columns; j++)
  1814.         msg_putchar('-');
  1815.     }
  1816.  
  1817.     print_line(i, FALSE);
  1818.  
  1819.     if (minus && (i == line))
  1820.     {
  1821.         msg_putchar('\n');
  1822.  
  1823.         for (j = 1; j < Columns; j++)
  1824.         msg_putchar('-');
  1825.     }
  1826.     }
  1827.  
  1828.     curwin->w_cursor.lnum = curs;
  1829. }
  1830.  
  1831. /*
  1832.  * Check if the restricted flag is set.
  1833.  * If so, give an error message and return TRUE.
  1834.  * Otherwise, return FALSE.
  1835.  */
  1836.     int
  1837. check_restricted()
  1838. {
  1839.     if (restricted)
  1840.     {
  1841.     EMSG("Shell commands not allowed in rvim");
  1842.     return TRUE;
  1843.     }
  1844.     return FALSE;
  1845. }
  1846.  
  1847. /*
  1848.  * Check if the secure flag is set (.exrc or .vimrc in current directory).
  1849.  * If so, give an error message and return TRUE.
  1850.  * Otherwise, return FALSE.
  1851.  */
  1852.     int
  1853. check_secure()
  1854. {
  1855.     if (secure)
  1856.     {
  1857.     secure = 2;
  1858.     emsg(e_curdir);
  1859.     return TRUE;
  1860.     }
  1861.     return FALSE;
  1862. }
  1863.  
  1864. static char_u        *old_sub = NULL;    /* previous substitute pattern */
  1865.  
  1866. /*
  1867.  * When ":global" is used to number of substitutions and changed lines is
  1868.  * accumulated until it's finished.
  1869.  */
  1870. static long        sub_nsubs;        /* total number of substitutions */
  1871. static linenr_t        sub_nlines;        /* total number of lines changed */
  1872.  
  1873. /* do_sub()
  1874.  *
  1875.  * Perform a substitution from line eap->line1 to line eap->line2 using the
  1876.  * command pointed to by eap->arg which should be of the form:
  1877.  *
  1878.  * /pattern/substitution/gc
  1879.  *
  1880.  * The trailing 'g' is optional and, if present, indicates that multiple
  1881.  * substitutions should be performed on each line, if applicable.
  1882.  * The trailing 'c' is optional and, if present, indicates that a confirmation
  1883.  * will be asked for each replacement.
  1884.  * The usual escapes are supported as described in the regexp docs.
  1885.  */
  1886.     void
  1887. do_sub(eap)
  1888.     EXARG    *eap;
  1889. {
  1890.     linenr_t        lnum;
  1891.     long        i;
  1892.     char_u       *ptr;
  1893.     char_u       *old_line;
  1894.     vim_regexp       *prog;
  1895.     static int        do_all = FALSE;    /* do multiple substitutions per line */
  1896.     static int        do_ask = FALSE;    /* ask for confirmation */
  1897.     int            do_error = TRUE;    /* if false, ignore errors */
  1898.     int            do_print = FALSE;    /* print last line with subst. */
  1899.     char_u       *pat = NULL, *sub = NULL;    /* init for GCC */
  1900.     int            delimiter;
  1901.     int            sublen;
  1902.     int            got_quit = FALSE;
  1903.     int            got_match = FALSE;
  1904.     int            temp;
  1905.     int            which_pat;
  1906.     char_u        *cmd;
  1907.     int            save_reg_ic;
  1908.  
  1909.     cmd = eap->arg;
  1910.     if (!global_busy)
  1911.     {
  1912.     sub_nsubs = 0;
  1913.     sub_nlines = 0;
  1914.     }
  1915.  
  1916. #ifdef FKMAP            /* reverse the flow of the Farsi characters */
  1917.     if (p_altkeymap && curwin->w_p_rl)
  1918.     lrF_sub(cmd);
  1919. #endif
  1920.  
  1921.     if (eap->cmdidx == CMD_tilde)
  1922.     which_pat = RE_LAST;    /* use last used regexp */
  1923.     else
  1924.     which_pat = RE_SUBST;    /* use last substitute regexp */
  1925.  
  1926.                 /* new pattern and substitution */
  1927.     if (eap->cmdidx == CMD_substitute && *cmd != NUL && !vim_iswhite(*cmd)
  1928.             && vim_strchr((char_u *)"0123456789gcr|\"", *cmd) == NULL)
  1929.     {
  1930.                 /* don't accept alphanumeric for separator */
  1931.     if (isalpha(*cmd))
  1932.     {
  1933.         EMSG("Regular expressions can't be delimited by letters");
  1934.         return;
  1935.     }
  1936.     /*
  1937.      * undocumented vi feature:
  1938.      *  "\/sub/" and "\?sub?" use last used search pattern (almost like
  1939.      *  //sub/r).  "\&sub&" use last substitute pattern (like //sub/).
  1940.      */
  1941.     if (*cmd == '\\')
  1942.     {
  1943.         ++cmd;
  1944.         if (vim_strchr((char_u *)"/?&", *cmd) == NULL)
  1945.         {
  1946.         emsg(e_backslash);
  1947.         return;
  1948.         }
  1949.         if (*cmd != '&')
  1950.         which_pat = RE_SEARCH;        /* use last '/' pattern */
  1951.         pat = (char_u *)"";            /* empty search pattern */
  1952.         delimiter = *cmd++;            /* remember delimiter character */
  1953.     }
  1954.     else        /* find the end of the regexp */
  1955.     {
  1956.         which_pat = RE_LAST;        /* use last used regexp */
  1957.         delimiter = *cmd++;            /* remember delimiter character */
  1958.         pat = cmd;                /* remember start of search pat */
  1959.         cmd = skip_regexp(cmd, delimiter, (int)p_magic);
  1960.         if (cmd[0] == delimiter)        /* end delimiter found */
  1961.         *cmd++ = NUL;            /* replace it with a NUL */
  1962.     }
  1963.  
  1964.     /*
  1965.      * Small incompatibility: vi sees '\n' as end of the command, but in
  1966.      * Vim we want to use '\n' to find/substitute a NUL.
  1967.      */
  1968.     sub = cmd;        /* remember the start of the substitution */
  1969.  
  1970.     while (cmd[0])
  1971.     {
  1972.         if (cmd[0] == delimiter)        /* end delimiter found */
  1973.         {
  1974.         *cmd++ = NUL;            /* replace it with a NUL */
  1975.         break;
  1976.         }
  1977.         if (cmd[0] == '\\' && cmd[1] != 0)    /* skip escaped characters */
  1978.         ++cmd;
  1979.         ++cmd;
  1980.     }
  1981.  
  1982.     if (!eap->skip)
  1983.     {
  1984.         vim_free(old_sub);
  1985.         old_sub = vim_strsave(sub);
  1986.     }
  1987.     }
  1988.     else if (!eap->skip)    /* use previous pattern and substitution */
  1989.     {
  1990.     if (old_sub == NULL)    /* there is no previous command */
  1991.     {
  1992.         emsg(e_nopresub);
  1993.         return;
  1994.     }
  1995.     pat = NULL;        /* search_regcomp() will use previous pattern */
  1996.     sub = old_sub;
  1997.     }
  1998.  
  1999.     /*
  2000.      * find trailing options
  2001.      */
  2002.     if (!p_ed)
  2003.     {
  2004.     if (p_gd)        /* default is global on */
  2005.         do_all = TRUE;
  2006.     else
  2007.         do_all = FALSE;
  2008.     do_ask = FALSE;
  2009.     }
  2010.     while (*cmd)
  2011.     {
  2012.     /*
  2013.      * Note that 'g' and 'c' are always inverted, also when p_ed is off
  2014.      * 'r' is never inverted.
  2015.      */
  2016.     if (*cmd == 'g')
  2017.         do_all = !do_all;
  2018.     else if (*cmd == 'c')
  2019.         do_ask = !do_ask;
  2020.     else if (*cmd == 'e')
  2021.         do_error = !do_error;
  2022.     else if (*cmd == 'r')        /* use last used regexp */
  2023.         which_pat = RE_LAST;
  2024.     else if (*cmd == 'p')
  2025.         do_print = TRUE;
  2026.     else
  2027.         break;
  2028.     ++cmd;
  2029.     }
  2030.  
  2031.     /*
  2032.      * check for a trailing count
  2033.      */
  2034.     cmd = skipwhite(cmd);
  2035.     if (isdigit(*cmd))
  2036.     {
  2037.     i = getdigits(&cmd);
  2038.     if (i <= 0 && !eap->skip && do_error)
  2039.     {
  2040.         emsg(e_zerocount);
  2041.         return;
  2042.     }
  2043.     eap->line1 = eap->line2;
  2044.     eap->line2 += i - 1;
  2045.     }
  2046.  
  2047.     /*
  2048.      * check for trailing command or garbage
  2049.      */
  2050.     cmd = skipwhite(cmd);
  2051.     if (*cmd && *cmd != '\"')        /* if not end-of-line or comment */
  2052.     {
  2053.     eap->nextcmd = check_nextcmd(cmd);
  2054.     if (eap->nextcmd == NULL)
  2055.     {
  2056.         emsg(e_trailing);
  2057.         return;
  2058.     }
  2059.     }
  2060.  
  2061.     if (eap->skip)        /* not executing commands, only parsing */
  2062.     return;
  2063.  
  2064.     if ((prog = search_regcomp(pat, RE_SUBST, which_pat, SEARCH_HIS)) == NULL)
  2065.     {
  2066.     if (do_error)
  2067.         emsg(e_invcmd);
  2068.     return;
  2069.     }
  2070.  
  2071.     /*
  2072.      * ~ in the substitute pattern is replaced with the old pattern.
  2073.      * We do it here once to avoid it to be replaced over and over again.
  2074.      */
  2075.     sub = regtilde(sub, (int)p_magic);
  2076.  
  2077.     old_line = NULL;
  2078.     for (lnum = eap->line1; lnum <= eap->line2 && !(got_int || got_quit);
  2079.                                        ++lnum)
  2080.     {
  2081.     ptr = ml_get(lnum);
  2082.     if (vim_regexec(prog, ptr, TRUE))  /* a match on this line */
  2083.     {
  2084.         char_u    *new_end, *new_start = NULL;
  2085.         char_u    *old_match, *old_copy;
  2086.         char_u    *prev_old_match = NULL;
  2087.         char_u    *p1;
  2088.         int        did_sub = FALSE;
  2089.         int        match, lastone;
  2090.         unsigned    len, needed_len;
  2091.         unsigned    new_start_len = 0;
  2092.  
  2093.         /* make a copy of the line, so it won't be taken away when updating
  2094.         the screen */
  2095.         if ((old_line = vim_strsave(ptr)) == NULL)
  2096.         continue;
  2097.         vim_regexec(prog, old_line, TRUE);    /* match again on this line to
  2098.                          * update the pointers. TODO:
  2099.                          * remove extra vim_regexec() */
  2100.         if (!got_match)
  2101.         {
  2102.         setpcmark();
  2103.         got_match = TRUE;
  2104.         }
  2105.  
  2106.         old_copy = old_match = old_line;
  2107.         for (;;)        /* loop until nothing more to replace */
  2108.         {
  2109.         /*
  2110.          * Save the position of the last change for the final cursor
  2111.          * position (just like the real vi).
  2112.          */
  2113.         curwin->w_cursor.lnum = lnum;
  2114.         curwin->w_cursor.col = (int)(prog->startp[0] - old_line);
  2115.         changed_cline_bef_curs();
  2116.  
  2117.         /*
  2118.          * Match empty string does not count, except for first match.
  2119.          * This reproduces the strange vi behaviour.
  2120.          * This also catches endless loops.
  2121.          */
  2122.         if (old_match == prev_old_match && old_match == prog->endp[0])
  2123.         {
  2124.             ++old_match;
  2125.             goto skip;
  2126.         }
  2127.         old_match = prog->endp[0];
  2128.         prev_old_match = old_match;
  2129.  
  2130.         /* update_screen() may change reg_ic: save it */
  2131.         save_reg_ic = reg_ic;
  2132.  
  2133.         /*
  2134.          * Loop until 'y', 'n', 'q', CTRL-E or CTRL-Y typed.
  2135.          */
  2136.         while (do_ask)
  2137.         {
  2138.             temp = RedrawingDisabled;
  2139.             RedrawingDisabled = FALSE;
  2140.             search_match_len = prog->endp[0] - prog->startp[0];
  2141.             /* invert the matched string
  2142.              * remove the inversion afterwards */
  2143.             if (search_match_len == 0)
  2144.             search_match_len = 1;        /* show something! */
  2145.             highlight_match = TRUE;
  2146.             update_topline();
  2147.             validate_cursor();
  2148.             update_screen(NOT_VALID);
  2149.             highlight_match = FALSE;
  2150.             redraw_later(NOT_VALID);
  2151.             if (msg_row == Rows - 1)
  2152.             msg_didout = FALSE;        /* avoid a scroll-up */
  2153.             /* write message same highlighting as for wait_return */
  2154.             smsg_attr(highlight_attr[HLF_R],
  2155.                 (char_u *)"replace with %s (y/n/a/q/^E/^Y)?",
  2156.                 sub);
  2157.             showruler(TRUE);
  2158.             RedrawingDisabled = temp;
  2159.  
  2160. #ifdef USE_GUI_WIN32
  2161.             dont_scroll = FALSE;    /* allow scrolling here */
  2162. #endif
  2163.             ++no_mapping;        /* don't map this key */
  2164.             i = vgetc();
  2165.             --no_mapping;
  2166.  
  2167.             /* clear the question */
  2168.             msg_didout = FALSE;        /* don't scroll up */
  2169.             msg_col = 0;
  2170.             gotocmdline(TRUE);
  2171.             if (i == 'q' || i == ESC || i == Ctrl('C')
  2172. #ifdef UNIX
  2173.                 || i == intr_char
  2174. #endif
  2175.                 )
  2176.             {
  2177.             got_quit = TRUE;
  2178.             break;
  2179.             }
  2180.             else if (i == 'n')
  2181.             goto skip;
  2182.             else if (i == 'y')
  2183.             break;
  2184.             else if (i == 'a')
  2185.             {
  2186.             do_ask = FALSE;
  2187.             break;
  2188.             }
  2189.             else if (i == Ctrl('E'))
  2190.             scrollup_clamp();
  2191.             else if (i == Ctrl('Y'))
  2192.             scrolldown_clamp();
  2193.         }
  2194.         if (got_quit)
  2195.             break;
  2196.  
  2197.         reg_ic = save_reg_ic;
  2198.  
  2199.         /* get length of substitution part */
  2200.         sublen = vim_regsub(prog, sub, old_line, FALSE, (int)p_magic);
  2201.         if (new_start == NULL)
  2202.         {
  2203.             /*
  2204.              * Get some space for a temporary buffer to do the
  2205.              * substitution into (and some extra space to avoid
  2206.              * too many calls to alloc()/free()).
  2207.              */
  2208.             new_start_len = STRLEN(old_copy) + sublen + 25;
  2209.             if ((new_start = alloc_check(new_start_len)) == NULL)
  2210.             goto outofmem;
  2211.             *new_start = NUL;
  2212.             new_end = new_start;
  2213.         }
  2214.         else
  2215.         {
  2216.             /*
  2217.              * Extend the temporary buffer to do the substitution into.
  2218.              * Avoid an alloc()/free(), it takes a lot of time.
  2219.              */
  2220.             len = STRLEN(new_start);
  2221.             needed_len = len + STRLEN(old_copy) + sublen + 1;
  2222.             if (needed_len > new_start_len)
  2223.             {
  2224.             needed_len += 20;    /* get some extra */
  2225.             if ((p1 = alloc_check(needed_len)) == NULL)
  2226.                 goto outofmem;
  2227.             STRCPY(p1, new_start);
  2228.             vim_free(new_start);
  2229.             new_start = p1;
  2230.             new_start_len = needed_len;
  2231.             }
  2232.             new_end = new_start + len;
  2233.         }
  2234.  
  2235.         /*
  2236.          * copy the text up to the part that matched
  2237.          */
  2238.         i = prog->startp[0] - old_copy;
  2239.         vim_memmove(new_end, old_copy, (size_t)i);
  2240.         new_end += i;
  2241.  
  2242.         vim_regsub(prog, sub, new_end, TRUE, (int)p_magic);
  2243.         sub_nsubs++;
  2244.         did_sub = TRUE;
  2245.  
  2246.         /*
  2247.          * Now the trick is to replace CTRL-Ms with a real line break.
  2248.          * This would make it impossible to insert CTRL-Ms in the text.
  2249.          * That is the way vi works. In Vim the line break can be
  2250.          * avoided by preceding the CTRL-M with a CTRL-V. Now you can't
  2251.          * precede a line break with a CTRL-V, big deal.
  2252.          */
  2253.         while ((p1 = vim_strchr(new_end, CR)) != NULL)
  2254.         {
  2255.             if (p1 == new_end || p1[-1] != Ctrl('V'))
  2256.             {
  2257.             if (u_inssub(lnum) == OK)   /* prepare for undo */
  2258.             {
  2259.                 *p1 = NUL;            /* truncate up to the CR */
  2260.                 mark_adjust(lnum, (linenr_t)MAXLNUM, 1L, 0L);
  2261.                 ml_append(lnum - 1, new_start,
  2262.                     (colnr_t)(p1 - new_start + 1), FALSE);
  2263.                 ++lnum;
  2264.                 ++eap->line2;    /* number of lines increases */
  2265.                 STRCPY(new_start, p1 + 1);    /* copy the rest */
  2266.                 new_end = new_start;
  2267.             }
  2268.             }
  2269.             else                /* remove CTRL-V */
  2270.             {
  2271.             STRCPY(p1 - 1, p1);
  2272.             new_end = p1;
  2273.             }
  2274.         }
  2275.  
  2276.         /* remember next character to be copied */
  2277.         old_copy = prog->endp[0];
  2278.         /*
  2279.          * continue searching after the match
  2280.          * prevent endless loop with patterns that match empty strings,
  2281.          * e.g. :s/$/pat/g or :s/[a-z]* /(&)/g
  2282.          */
  2283. skip:
  2284.         match = -1;
  2285.         lastone = (*old_match == NUL || got_int || got_quit || !do_all);
  2286.         if (lastone || do_ask ||
  2287.               (match = vim_regexec(prog, old_match, (int)FALSE)) == 0)
  2288.         {
  2289.             if (new_start)
  2290.             {
  2291.             /*
  2292.              * Copy the rest of the line, that didn't match.
  2293.              * Old_match has to be adjusted, we use the end of the
  2294.              * line as reference, because the substitute may have
  2295.              * changed the number of characters.
  2296.              */
  2297.             STRCAT(new_start, old_copy);
  2298.             i = old_line + STRLEN(old_line) - old_match;
  2299.             if (u_savesub(lnum) == OK)
  2300.                 ml_replace(lnum, new_start, TRUE);
  2301.             /* When asking, undo is saved each time, must also set
  2302.              * changed flag each time. */
  2303.             if (do_ask)
  2304.                 CHANGED;
  2305.  
  2306.             vim_free(old_line);        /* free the temp buffer */
  2307.             old_line = new_start;
  2308.             new_start = NULL;
  2309.             old_match = old_line + STRLEN(old_line) - i;
  2310.             if (old_match < old_line)    /* safety check */
  2311.             {
  2312.                 EMSG("do_sub internal error: old_match < old_line");
  2313.                 old_match = old_line;
  2314.             }
  2315.             old_copy = old_line;
  2316.             }
  2317.             if (match == -1 && !lastone)
  2318.             match = vim_regexec(prog, old_match, (int)FALSE);
  2319.             if (match <= 0)   /* quit loop if there is no more match */
  2320.             break;
  2321.         }
  2322.         line_breakcheck();
  2323.  
  2324.         }
  2325.         if (did_sub)
  2326.         ++sub_nlines;
  2327.         vim_free(old_line);        /* free the copy of the original line */
  2328.         old_line = NULL;
  2329.     }
  2330.     line_breakcheck();
  2331.     }
  2332.     curbuf->b_op_start.lnum = eap->line1;
  2333.     curbuf->b_op_end.lnum = eap->line2;
  2334.     curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
  2335.  
  2336. outofmem:
  2337.     vim_free(old_line);        /* may have to free an allocated copy of the line */
  2338.     if (sub_nsubs)
  2339.     {
  2340.     CHANGED;
  2341.     approximate_botline();
  2342.     if (!global_busy)
  2343.     {
  2344.         update_topline();
  2345.         beginline(BL_WHITE | BL_FIX);
  2346.         update_screen(NOT_VALID); /* need this to update LineSizes */
  2347.         if (!do_sub_msg() && do_ask)
  2348.         MSG("");
  2349.     }
  2350.     if (do_print)
  2351.         print_line(curwin->w_cursor.lnum, FALSE);
  2352.     }
  2353.     else if (!global_busy)
  2354.     {
  2355.     if (got_int)        /* interrupted */
  2356.         emsg(e_interr);
  2357.     else if (got_match)    /* did find something but nothing substituted */
  2358.         MSG("");
  2359.     else if (do_error)    /* nothing found */
  2360.         emsg2(e_patnotf2, pat);
  2361.     }
  2362.  
  2363.     vim_free(prog);
  2364. }
  2365.  
  2366. /*
  2367.  * Give message for number of substitutions.
  2368.  * Can also be used after a ":global" command.
  2369.  * Return TRUE if a message was given.
  2370.  */
  2371.     static int
  2372. do_sub_msg()
  2373. {
  2374.     /*
  2375.      * Only report substitutions when:
  2376.      * - more than 'report' substitutions
  2377.      * - command was typed by user, or number of changed lines > 'report'
  2378.      * - giving messages is not disabled by 'lazyredraw'
  2379.      */
  2380.     if (sub_nsubs > p_report &&
  2381.         (KeyTyped || sub_nlines > 1 || p_report < 1) &&
  2382.         messaging())
  2383.     {
  2384.     sprintf((char *)msg_buf, "%s%ld substitution%s on %ld line%s",
  2385.         got_int ? "(Interrupted) " : "",
  2386.         sub_nsubs, plural(sub_nsubs),
  2387.         (long)sub_nlines, plural((long)sub_nlines));
  2388.     if (msg(msg_buf))
  2389.     {
  2390.         keep_msg = msg_buf;
  2391.         keep_msg_attr = 0;
  2392.     }
  2393.     return TRUE;
  2394.     }
  2395.     if (got_int)
  2396.     {
  2397.     emsg(e_interr);
  2398.     return TRUE;
  2399.     }
  2400.     return FALSE;
  2401. }
  2402.  
  2403. /*
  2404.  * do_glob(cmd)
  2405.  *
  2406.  * Execute a global command of the form:
  2407.  *
  2408.  * g/pattern/X : execute X on all lines where pattern matches
  2409.  * v/pattern/X : execute X on all lines where pattern does not match
  2410.  *
  2411.  * where 'X' is an EX command
  2412.  *
  2413.  * The command character (as well as the trailing slash) is optional, and
  2414.  * is assumed to be 'p' if missing.
  2415.  *
  2416.  * This is implemented in two passes: first we scan the file for the pattern and
  2417.  * set a mark for each line that (not) matches. secondly we execute the command
  2418.  * for each line that has a mark. This is required because after deleting
  2419.  * lines we do not know where to search for the next match.
  2420.  */
  2421.     void
  2422. do_glob(eap)
  2423.     EXARG    *eap;
  2424. {
  2425.     linenr_t        lnum;    /* line number according to old situation */
  2426.     linenr_t        old_lcount; /* b_ml.ml_line_count before the command */
  2427.     int            ndone;
  2428.     int            type;    /* first char of cmd: 'v' or 'g' */
  2429.     char_u        *cmd;    /* command argument */
  2430.  
  2431.     char_u        delim;    /* delimiter, normally '/' */
  2432.     char_u       *pat;
  2433.     vim_regexp       *prog;
  2434.     int            match;
  2435.     int            which_pat;
  2436.  
  2437.     if (global_busy)
  2438.     {
  2439.     EMSG("Cannot do :global recursive");    /* will increment global_busy */
  2440.     return;
  2441.     }
  2442.  
  2443.     type = *eap->cmd;
  2444.     cmd = eap->arg;
  2445.     which_pat = RE_LAST;        /* default: use last used regexp */
  2446.     sub_nsubs = 0;
  2447.     sub_nlines = 0;
  2448.  
  2449.     /*
  2450.      * undocumented vi feature:
  2451.      *    "\/" and "\?": use previous search pattern.
  2452.      *         "\&": use previous substitute pattern.
  2453.      */
  2454.     if (*cmd == '\\')
  2455.     {
  2456.     ++cmd;
  2457.     if (vim_strchr((char_u *)"/?&", *cmd) == NULL)
  2458.     {
  2459.         emsg(e_backslash);
  2460.         return;
  2461.     }
  2462.     if (*cmd == '&')
  2463.         which_pat = RE_SUBST;    /* use previous substitute pattern */
  2464.     else
  2465.         which_pat = RE_SEARCH;    /* use previous search pattern */
  2466.     ++cmd;
  2467.     pat = (char_u *)"";
  2468.     }
  2469.     else if (*cmd == NUL)
  2470.     {
  2471.     EMSG("Regular expression missing from global");
  2472.     return;
  2473.     }
  2474.     else
  2475.     {
  2476.     delim = *cmd;        /* get the delimiter */
  2477.     if (delim)
  2478.         ++cmd;        /* skip delimiter if there is one */
  2479.     pat = cmd;        /* remember start of pattern */
  2480.     cmd = skip_regexp(cmd, delim, (int)p_magic);
  2481.     if (cmd[0] == delim)            /* end delimiter found */
  2482.         *cmd++ = NUL;            /* replace it with a NUL */
  2483.     }
  2484.  
  2485. #ifdef FKMAP        /* when in Farsi mode, reverse the character flow */
  2486.     if (p_altkeymap && curwin->w_p_rl)
  2487.     lrFswap(pat,0);
  2488. #endif
  2489.  
  2490.     if ((prog = search_regcomp(pat, RE_BOTH, which_pat, SEARCH_HIS)) == NULL)
  2491.     {
  2492.     emsg(e_invcmd);
  2493.     return;
  2494.     }
  2495.  
  2496. /*
  2497.  * pass 1: set marks for each (not) matching line
  2498.  */
  2499.     ndone = 0;
  2500.     for (lnum = eap->line1; lnum <= eap->line2 && !got_int; ++lnum)
  2501.     {
  2502.     /* a match on this line? */
  2503.     match = vim_regexec(prog, ml_get(lnum), (int)TRUE);
  2504.     if ((type == 'g' && match) || (type == 'v' && !match))
  2505.     {
  2506.         ml_setmarked(lnum);
  2507.         ndone++;
  2508.     }
  2509.     line_breakcheck();
  2510.     }
  2511.  
  2512. /*
  2513.  * pass 2: execute the command for each line that has been marked
  2514.  */
  2515.     if (got_int)
  2516.     MSG(e_interr);
  2517.     else if (ndone == 0)
  2518.     smsg(e_patnotf2, pat);
  2519.     else
  2520.     {
  2521.     /*
  2522.      * Set current position only once for a global command.
  2523.      * If global_busy is set, setpcmark() will not do anything.
  2524.      * If there is an error, global_busy will be incremented.
  2525.      */
  2526.     setpcmark();
  2527.  
  2528.     global_busy = 1;
  2529.     old_lcount = curbuf->b_ml.ml_line_count;
  2530.     while (!got_int && (lnum = ml_firstmarked()) != 0 && global_busy == 1)
  2531.     {
  2532.         curwin->w_cursor.lnum = lnum;
  2533.         curwin->w_cursor.col = 0;
  2534.         if (*cmd == NUL || *cmd == '\n')
  2535.         do_cmdline((char_u *)"p", NULL, NULL, DOCMD_NOWAIT);
  2536.         else
  2537.         do_cmdline(cmd, NULL, NULL, DOCMD_NOWAIT);
  2538.         ui_breakcheck();
  2539.     }
  2540.  
  2541.     global_busy = 0;
  2542.     adjust_cursor();    /* cursor may be beyond the end of the line */
  2543.  
  2544.     /*
  2545.      * Redraw everything.  Could use CLEAR, which is faster in some
  2546.      * situations, but when there are few changes this makes the display
  2547.      * flicker.
  2548.      */
  2549.     redraw_later(NOT_VALID);
  2550.  
  2551.     /* If subsitutes done, report number of substitues, otherwise report
  2552.      * number of extra or deleted lines. */
  2553.     if (!do_sub_msg())
  2554.         msgmore(curbuf->b_ml.ml_line_count - old_lcount);
  2555.     }
  2556.  
  2557.     ml_clearmarked();       /* clear rest of the marks */
  2558.     vim_free(prog);
  2559. }
  2560.  
  2561. #ifdef VIMINFO
  2562.     int
  2563. read_viminfo_sub_string(line, fp, force)
  2564.     char_u  *line;
  2565.     FILE    *fp;
  2566.     int        force;
  2567. {
  2568.     if (old_sub != NULL && force)
  2569.     vim_free(old_sub);
  2570.     if (force || old_sub == NULL)
  2571.     {
  2572.     viminfo_readstring(line);
  2573.     old_sub = vim_strsave(line + 1);
  2574.     }
  2575.     return vim_fgets(line, LSIZE, fp);
  2576. }
  2577.  
  2578.     void
  2579. write_viminfo_sub_string(fp)
  2580.     FILE    *fp;
  2581. {
  2582.     if (get_viminfo_parameter('/') != 0 && old_sub != NULL)
  2583.     {
  2584.     fprintf(fp, "\n# Last Substitute String:\n$");
  2585.     viminfo_writestring(fp, old_sub);
  2586.     }
  2587. }
  2588. #endif /* VIMINFO */
  2589.  
  2590.  
  2591. /*
  2592.  * ":help": open a read-only window on the help.txt file
  2593.  */
  2594.     void
  2595. do_help(eap)
  2596.     EXARG   *eap;
  2597. {
  2598.     char_u  *arg;
  2599.     FILE    *helpfd;        /* file descriptor of help file */
  2600.     int        n;
  2601.     WIN        *wp;
  2602.     int        num_matches;
  2603.     char_u  **matches;
  2604.     int        need_free = FALSE;
  2605.  
  2606.     if (eap != NULL)
  2607.     {
  2608.     /*
  2609.      * A ":help" command ends at the first LF, or at a '|' that is
  2610.      * followed by some text.  Set nextcmd to the following command.
  2611.      */
  2612.     for (arg = eap->arg; *arg; ++arg)
  2613.     {
  2614.         if (*arg == '\n' || *arg == '\r' || (*arg == '|' && arg[1] != NUL))
  2615.         {
  2616.         *arg++ = NUL;
  2617.         eap->nextcmd = arg;
  2618.         break;
  2619.         }
  2620.     }
  2621.     arg = eap->arg;
  2622.  
  2623.     if (eap->skip)        /* not executing commands */
  2624.         return;
  2625.     }
  2626.     else
  2627.     arg = (char_u *)"";
  2628.  
  2629.     /*
  2630.      * If an argument is given, check if there is a match for it.
  2631.      */
  2632.     if (*arg != NUL)
  2633.     {
  2634.     n = find_help_tags(arg, &num_matches, &matches);
  2635.     if (num_matches == 0 || n == FAIL)
  2636.     {
  2637.         EMSG2("Sorry, no help for %s", arg);
  2638.         return;
  2639.     }
  2640.  
  2641.     /* The first match is the best match. */
  2642.     arg = vim_strsave(matches[0]);
  2643.     need_free = TRUE;
  2644.     FreeWild(num_matches, matches);
  2645.     }
  2646.  
  2647.     /*
  2648.      * If there is already a help window open, use that one.
  2649.      */
  2650.     if (!curwin->w_buffer->b_help)
  2651.     {
  2652.     for (wp = firstwin; wp != NULL; wp = wp->w_next)
  2653.         if (wp->w_buffer != NULL && wp->w_buffer->b_help)
  2654.         break;
  2655.     if (wp != NULL && wp->w_buffer->b_nwindows > 0)
  2656.         win_enter(wp, TRUE);
  2657.     else
  2658.     {
  2659.         /*
  2660.          * There is no help buffer yet.
  2661.          * Try to open the file specified by the "helpfile" option.
  2662.          */
  2663.         if ((helpfd = fopen((char *)p_hf, READBIN)) == NULL)
  2664.         {
  2665.         smsg((char_u *)"Sorry, help file \"%s\" not found", p_hf);
  2666.         goto erret;
  2667.         }
  2668.         fclose(helpfd);
  2669.  
  2670.         if (win_split(0, FALSE, FALSE) == FAIL)
  2671.         goto erret;
  2672.  
  2673.         if (curwin->w_height < p_hh)
  2674.         win_setheight((int)p_hh);
  2675.  
  2676. #ifdef RIGHTLEFT
  2677.         curwin->w_p_rl = 0;            /* help window is left-to-right */
  2678. #endif
  2679.         curwin->w_p_nu = 0;            /* no line numbers */
  2680.  
  2681.         /* Save the values of the options we will change.  Do this before
  2682.          * do_ecmd(), because there could be modelines in the help file */
  2683.         vim_free(help_save_isk);
  2684.         help_save_isk = vim_strsave(curbuf->b_p_isk);
  2685.         help_save_ts = curbuf->b_p_ts;
  2686.  
  2687.         /*
  2688.          * open help file (do_ecmd() will set b_help flag, readfile() will
  2689.          * set b_p_ro flag)
  2690.          */
  2691.         (void)do_ecmd(0, p_hf, NULL, NULL, (linenr_t)0,
  2692.                            ECMD_HIDE + ECMD_SET_HELP);
  2693.  
  2694.         /* accept all chars for keywords, except ' ', '*', '"', '|' */
  2695.         set_string_option_direct((char_u *)"isk", -1,
  2696.                          (char_u *)"!-~,^*,^|,^\"", TRUE);
  2697.         curbuf->b_p_ts = 8;
  2698.         curwin->w_p_list = FALSE;
  2699.         check_buf_options(curbuf);
  2700.         (void)init_chartab();    /* needed because 'isk' changed */
  2701.     }
  2702.     }
  2703.  
  2704.     if (!p_im)
  2705.     restart_edit = 0;        /* don't want insert mode in help file */
  2706.  
  2707.     if (arg == NULL || *arg == NUL)
  2708.     {
  2709.     arg = (char_u *)"help.txt";        /* go to the index */
  2710.     need_free = FALSE;
  2711.     }
  2712.     do_tag(arg, DT_HELP, 1, FALSE);
  2713.  
  2714. erret:
  2715.     if (need_free)
  2716.     vim_free(arg);
  2717. }
  2718.  
  2719. /*
  2720.  * Return a heuristic indicating how well the given string matches.  The
  2721.  * smaller the number, the better the match.  This is the order of priorities,
  2722.  * from best match to worst match:
  2723.  *    - Match with least alpha-numeric characters is better.
  2724.  *    - Match with least total characters is better.
  2725.  *    - Match towards the start is better.
  2726.  * Assumption is made that the matched_string passed has already been found to
  2727.  * match some string for which help is requested.  webb.
  2728.  */
  2729.     int
  2730. help_heuristic(matched_string, offset, wrong_case)
  2731.     char_u  *matched_string;
  2732.     int        offset;        /* offset for match */
  2733.     int        wrong_case;        /* no matching case */
  2734. {
  2735.     int        num_letters;
  2736.     char_u  *p;
  2737.  
  2738.     num_letters = 0;
  2739.     for (p = matched_string; *p; p++)
  2740.     if (isalnum(*p))
  2741.         num_letters++;
  2742.  
  2743.     /*
  2744.      * Multiply the number of letters by 100 to give it a much bigger
  2745.      * weighting than the number of characters.
  2746.      * If there match is only a match while ignoring case, add 5000.
  2747.      * If the match starts in the middle of a word, add 10000 to put it
  2748.      * somewhere in the last half.
  2749.      * If the match is more than 2 chars from the start, multiply by 200 to
  2750.      * put it after matches at the start.
  2751.      */
  2752.     if (isalnum(matched_string[offset]) && offset > 0 &&
  2753.                       isalnum(matched_string[offset - 1]))
  2754.     offset += 10000;
  2755.     else if (offset > 2)
  2756.     offset *= 200;
  2757.     if (wrong_case)
  2758.     offset += 5000;
  2759.     return (int)(100 * num_letters + STRLEN(matched_string) + offset);
  2760. }
  2761.  
  2762. /*
  2763.  * Compare functions for qsort() below, that checks the help heuristics number
  2764.  * that has been put after the tagname by find_tags().
  2765.  */
  2766.     static int
  2767. #ifdef __BORLANDC__
  2768. _RTLENTRYF
  2769. #endif
  2770. help_compare(s1, s2)
  2771.     const void    *s1;
  2772.     const void    *s2;
  2773. {
  2774.     char    *p1;
  2775.     char    *p2;
  2776.  
  2777.     p1 = *(char **)s1 + strlen(*(char **)s1) + 1;
  2778.     p2 = *(char **)s2 + strlen(*(char **)s2) + 1;
  2779.     return strcmp(p1, p2);
  2780. }
  2781.  
  2782. /*
  2783.  * Find all help tags matching "arg", sort them and return in matches[], with
  2784.  * the number of matches in num_matches.
  2785.  * We try first with case, and then ignoring case.  Then we try to choose the
  2786.  * "best" match from the ones found.
  2787.  */
  2788.     int
  2789. find_help_tags(arg, num_matches, matches)
  2790.     char_u    *arg;
  2791.     int        *num_matches;
  2792.     char_u    ***matches;
  2793. {
  2794.     char_u    *s, *d;
  2795.     int        i;
  2796.     static char *(mtable[]) = {"*", "g*", "[*", "]*",
  2797.                    "/*", "/\\*", "/\\(\\)",
  2798.                    "?", ":?", "?<CR>", "\"*",
  2799.                    "[count]", "[quotex]", "[range]",
  2800.                    "[pattern]"};
  2801.     static char *(rtable[]) = {"star", "gstar", "[star", "]star",
  2802.                    "/star", "/\\\\star", "/\\\\(\\\\)",
  2803.                    "?", ":?", "?<CR>", "quotestar",
  2804.                    "\\[count]", "\\[quotex]", "\\[range]",
  2805.                    "\\[pattern]"};
  2806.  
  2807.     d = IObuff;            /* assume IObuff is long enough! */
  2808.  
  2809.     /*
  2810.      * Recognize a few exceptions to the rule.    Some strings that contain '*'
  2811.      * with "star".  Otherwise '*' is recognized as a wildcard.
  2812.      */
  2813.     for (i = sizeof(mtable) / sizeof(char *); --i >= 0; )
  2814.     {
  2815.     if (STRCMP(arg, mtable[i]) == 0)
  2816.     {
  2817.         STRCPY(d, rtable[i]);
  2818.         break;
  2819.     }
  2820.     }
  2821.  
  2822.     if (i < 0)        /* no match in table, replace single characters */
  2823.     {
  2824.     for (s = arg; *s; ++s)
  2825.     {
  2826.         /*
  2827.          * Replace "|" with "bar" and '"' with "quote" to match the name of
  2828.          * the tags for these commands.
  2829.          * Replace "*" with ".*" and "?" with "." to match command line
  2830.          * completion.
  2831.          * Insert a backslash before '~', '$' and '.' to avoid their
  2832.          * special meaning.
  2833.          */
  2834.         if (d - IObuff > IOSIZE - 10)    /* getting too long!? */
  2835.         break;
  2836.         switch (*s)
  2837.         {
  2838.         case '|':   STRCPY(d, "bar");
  2839.                 d += 3;
  2840.                 continue;
  2841.         case '\"':  STRCPY(d, "quote");
  2842.                 d += 5;
  2843.                 continue;
  2844.         case '*':   *d++ = '.';
  2845.                 break;
  2846.         case '?':   *d++ = '.';
  2847.                 continue;
  2848.         case '$':
  2849.         case '.':
  2850.         case '~':   *d++ = '\\';
  2851.                 break;
  2852.         }
  2853.  
  2854.         /*
  2855.          * Replace "^x" by "CTRL-X". Don't do this for "^_" to make
  2856.          * ":help i_^_CTRL-D" work.
  2857.          */
  2858.         if (*s < ' ' || (*s == '^' && s[1] && s[1] != '_'))    /* ^x */
  2859.         {
  2860.         if (d > IObuff && d[-1] != '_')
  2861.             *d++ = '_';        /* prepend a '_' */
  2862.         STRCPY(d, "CTRL-");
  2863.         d += 5;
  2864.         if (*s < ' ')
  2865.             *d++ = *s + '@';
  2866.         else
  2867.             *d++ = *++s;
  2868.         if (s[1] != NUL && s[1] != '_')
  2869.             *d++ = '_';        /* append a '_' */
  2870.         continue;
  2871.         }
  2872.         else if (*s == '^')        /* "^" or "CTRL-^" or "^_" */
  2873.         *d++ = '\\';
  2874.  
  2875.         /*
  2876.          * Insert a backslash before a backslash after a slash, for search
  2877.          * pattern tags: "/\|" --> "/\\|".
  2878.          */
  2879.         else if (s[0] == '\\' && s[1] != '\\' &&
  2880.                           *arg == '/' && s == arg + 1)
  2881.         *d++ = '\\';
  2882.  
  2883.         *d++ = *s;
  2884.  
  2885.         /*
  2886.          * If tag starts with ', toss everything after a second '. Fixes
  2887.          * CTRL-] on 'option'. (would include the trailing '.').
  2888.          */
  2889.         if (*s == '\'' && s > arg && *arg == '\'')
  2890.         break;
  2891.     }
  2892.     *d = NUL;
  2893.     }
  2894.  
  2895.     *matches = (char_u **)"";
  2896.     *num_matches = 0;
  2897.     if (find_tags(IObuff, num_matches, matches,
  2898.                  TAG_HELP | TAG_REGEXP | TAG_NAMES, MAXCOL) == OK)
  2899. #ifdef HAVE_QSORT
  2900.     /*
  2901.      * Sort the matches found on the heuristic number that is after the
  2902.      * tag name.  If there is no qsort, the output will be messy!
  2903.      */
  2904.     qsort((void *)*matches, (size_t)*num_matches,
  2905.                           sizeof(char_u *), help_compare)
  2906. #endif
  2907.     ;
  2908.     return OK;
  2909. }
  2910.